- This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
Viewing 7 posts - 1 through 7 (of 7 total)
- The forum ‘Back End’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
<?php if ( $name == Cummins || Freightliner) {
echo 'Trucks';
} elseif ( $name == Ford || Honda){
echo 'cars';
}
?>
Thanks
$name = 'Cummins';
// $name = 'Freightliner';
// $name = 'Ford';
// $name = 'Honda';
if ( $name == 'Cummins' || $name = 'Freightliner') {
echo 'Truck!';
} elseif ( $name == 'Ford' || $name == 'Honda' ) {
echo 'Cars!';
}
// Another way is to use arrays to hold data,
// then you can just check if value is present
$trucks = array('Cummins', 'Freightliner');
$cars = array('Ford', 'Honda');
// in_array( 'term', $array )
if ( in_array('Cummins', $trucks) ) {
echo 'Trucks!';
}
if ( in_array('Ford', $cars) ) {
echo 'Cars!';
}
Perfect thanks!
if ( $name == 'Cummins' || $name = 'Freightliner')
correction (typo?): should be double equals (==
) on both.
if ( in_array('Cummins', $trucks) ) {
echo 'Trucks!';
}
Isn’t it redundant to have Cummins and $trucks since Cummins is a truck. Why can’t you just do…
if ( in_array($trucks) ) {
echo 'Trucks!';
}
The first parameter of “in_array” is the value you’re checking for in the array, which is the second parameter. Without the first value, the function wouldn’t know what it’s looking for.
Why can’t you just do…
For future reference, this (and many more) questions can be answered by Reading The Friendly Manual.