Here is my If Else Statement
if(isset($row['content']) && strlen($row['content'])) {
$content = $row['content'];
}
elseif(isset($row['description']) && strlen($row['description'])) {
$content = $row['description'];
}
I tried to create a condition using ternerary operator and ended for with a error: Here is my ternerary condition
$content = isset($row['content']) && strlen($row['content']) ? $row['content'] : isset($row['description']) && strlen($row['description']) ? $row['description'] : '';
What is the correct statement?
You're making your code very very unreadable by changing your condition into a ternary operator. Anyhoo, the following works without an error.
$content = (isset($row['content']) && strlen($row['content']))
? $row['content']
: (isset($row['description']) && strlen($row['description'])
? $row['description']
: '');
Wrapped the last expression in parenthesis so PHP doesn't try to evaluate it separately.
Try putting inside bracket the first term of ?: and the last term of first ?:.
$content = (isset($row['content']) && strlen($row['content'])) ? $row['content'] : ((isset($row['description']) && strlen($row['description'])) ? $row['description'] : '');
Related
I know that is basic question, but i can't figure out how to test if $_GET['zanr'] contains '' or 'sve' to do other stuff in my script...i try using this code but it only checks '' and 'sve' is ignored...so how to check if 'zanr' contains '' or 'sve' do stuff a else do stuff b?
if (isset($_GET['zanr']) === '' || isset($_GET['zanr']) === 'sve'){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
I also try using ?? but no success...thanks.
Since your goal is to check if your $_GET is empty, use PHP built in function: empty(). And your second statement is wrong because isset() is returning a boolean and therefore you're not validating the string itself. So be sure to remove isset() and just compare if $_GET['zanr'] has your specific string.
Use this:
if (empty($_GET['zanr']) || $_GET['zanr'] == 'sve'){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
Try below code.
if (isset($_GET['zanr']) && ($_GET['zanr'] == '' || $_GET['zanr'] == 'sve')){
echo "zanr = '' or 'sve'";
} else {
echo "zanr contains other values...";
}
When coding with isset i am getting an fatal error.I have searched stackoverflow but results are not satisfactory.
I am getting
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
My codes are
if (!isset( $size || $color )) {
$style = '';
}else{
$style = 'font-size : ' . $size . ';color:' . $color;
}
As mentioned in the comments (and the error message), you cannot pass the result of an expression to isset.
You can use multiple isset calls, or reverse the logic of your if/else block and pass multiple parameters to isset, which i think is the cleanest solution:
//true if both are set
if(isset($size, $color)) {
$style = 'font-size : ' . $size . ';color:' . $color;
}else{
$style = '';
}
You can clean this up a little further by setting the default value first, thus avoiding the need for an else section:
$style = '';
if(isset($size, $color)) {
$style = 'font-size : ' . $size . ';color:' . $color;
}
You could even use a ternary, though some people find them harder to read:
$style = isset($size, $color) ? 'font-size : ' . $size . ';color:' . $color : '';
you should use this way
if (!isset( $size ) || !isset( $color )) {
Your expression always return either true or false => In theory isset always return true so PHP not allow this
Change
if (!isset( $size || $color )) {
To
if (!isset($size) || !isset($color)) {
I am trying to use an if statement in a variable in wordpress such that if the page-id is 17711 the variable should be equal to 4 if not true is should be equal to intval( get_option('wp_estate_prop_no', '') );
The code i am trying to use is:
$prop_no = if (is_page(17711)) { 4}
else {
intval( get_option('wp_estate_prop_no', '') );
}
I'm unsure what your is_page function does, but you should be able to use PHP's ternary operator to achieve an identical effect.
$prop_no = (is_page(17711) ? 4 : intval( get_option('wp_estate_prop_no', '')));
Try this:
$prop_no = (is_page(17711)?4:intval(get_option('wp_estate_prop_no,''));
Read about here : https://davidwalsh.name/php-ternary-examples
yo can use $proper_no = is_page(17711) ? 4 : intval( get_option('wp_estate_prop_no', ''));
or.
if(is_page(17711) == 4){
$proper_no = 4;
}else{
$proper_no = get_option('wp_estate_prop_no', '');
}
I have this condition here:
if($name != '' || $name != 0 || $name != "0"){
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
My problem with this condition if $name is equal to "0" it still adds the $where to my $where variable. how do I fix this ?
i think, you mean something else, try use && instead of ||
Well, if this statement is true:
$name is equal to "0"
Then this condition evaluates to true:
$name != ''
Therefore the entire conditional check evaluates to true.
It sounds like you want to use && instead of ||:
if($name != '' && $name != 0 && $name != "0")
That way the entire condition will evaluate to true only if all three conditions are met, instead of only one condition.
The best solution is to use :
if(!empty($name))
Just using this should solve your issue:
if($name){
//your condition
}
try this
if ( !in_array($name, array('','0',0), true ) ) {
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
I am trying to make this IF statement to work:
" . ($currentpage == '/adver.php' ? 'active' : '' || $currentpage == 'editad.php' ? 'active' : '' ) . "
What I want to do, is that if the $currentpage is /adver.php or /editad.php, then "active" should be printed out.
The above doesn't work. How can I make the IF statement to function correct?
Use basename() with $_SERVER['PHP_SELF'] to get the current script file name and than compare
if(basename($_SERVER['PHP_SELF']) == 'adver.php' || basename($_SERVER['PHP_SELF']) == 'editad.php') {
echo 'class="active"'; //Whatever you want to echo
}
You can simply create a function(I often use) to get the script file name like
function script_name() {
return basename($_SERVER['PHP_SELF']);
}
Now you can use something like
(script_name() == 'index.php') ? '' : '';
When I use this to echo out the active page I often use something like
function is_active($script_name) {
if(basename($_SERVER['PHP_SELF']) == $script_name) {
return 'class="active"';
}
}
Now you can simply use the above function say in your menu like
<a href="index.php" <?php echo is_active('index.php'); ?>>
I believe you're using the ternary operator incorrectly.
You may want to try the following code:
($currentpage == '/adver.php' || $currentpage == 'editad.php') ? 'active' : ''
Remember that the syntax of the ternary operator is the following:
(comparison) ? (if true) : (if false)
Therefore, the full comparison should go at the beginning. Anyway, consider using a normal if/else when possible, as ternary operators might be confusing both for the writer and the reader (and furthermore, you don't really need the else in this case).
EDIT: I recommend using Mr. Alien's solution.
if($currentpage == "adver.php" || $currentpage == "editad.php") {
echo "Active";
}
If you insist on using the if shorthand, you can try something like:
" . ($currentpage == '/adver.php' ? 'active' : ($currentpage == 'editad.php' ? 'active' : '')) . "
Alternatively, you could something like:
" . (in_array($currentpage, array('/adver.php', 'editad.php')) ? 'active' : '') . "
There is a mistake to use '(' and ')'
Your should use like this
" . ($currentpage == '/adver.php') ? 'active' : '' || ($currentpage == 'editad.php') ? 'active' : '' . "
instead of
" . ($currentpage == '/adver.php' ? 'active' : '' || $currentpage == 'editad.php' ? 'active' : '') . "