PHP shorthand for isset()? [duplicate]

Update for PHP 7 (thanks shock_gone_wild) PHP 7 introduces the null coalescing operator which simplifies the below statements to: $var = $var ?? “default”; Before PHP 7 No, there is no special operator or special syntax for this. However, you could use the ternary operator: $var = isset($var) ? $var : “default”; Or like this: … Read more

Check if value isset and null

IIRC, you can use get_defined_vars() for this: $foo = NULL; $vars = get_defined_vars(); if (array_key_exists(‘bar’, $vars)) {}; // Should evaluate to FALSE if (array_key_exists(‘foo’, $vars)) {}; // Should evaluate to TRUE

PHP: Check if variable exist but also if has a value equal to something

Sadly that’s the only way to do it. But there are approaches for dealing with larger arrays. For instance something like this: $required = array(‘myvar’, ‘foo’, ‘bar’, ‘baz’); $missing = array_diff($required, array_keys($_GET)); The variable $missing now contains a list of values that are required, but missing from the $_GET array. You can use the $missing … Read more

Calling a particular PHP function on form submit

In the following line <form method=”post” action=”display()”> the action should be the name of your script and you should call the function, Something like this <form method=”post” action=”yourFileName.php”> <input type=”text” name=”studentname”> <input type=”submit” value=”click” name=”submit”> <!– assign a name for the button –> </form> <?php function display() { echo “hello “.$_POST[“studentname”]; } if(isset($_POST[‘submit’])) { display(); … Read more

PHP Error : Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` [duplicate]

You need to add parentheses around your code: Before: $reference->frotel_vitrine = empty($item->special) ? null : $item->special == 2 || $item->special == 3 ? ‘active’ : ‘deactivate’; After : $reference->frotel_vitrine = empty($item->special) ? null : (($item->special == 2 || $item->special == 3 )? ‘active’ : ‘deactivate’); That should solve the issue.