If I were to call:
www.*.com?hello
if($_GET["hello"]){
}
It will always return false because ?hello variable needs to be set as something...
Is there a way to get the ? part without setting the variable to anythign before hand?
You can check if variable is set like this:
if(isset($_GET["hello"])){
}
sometimes key_exists() is better because if $_GET["hello"] == null you can get false
if (key_exists("hello", $_GET)) {
}
$_GET["hello"] is falsy, check if it's set at all
if (isset($_GET["hello"])) {
//do stuff
}
Use array_key_exists:
if (array_key_exists("hello", $_GET)) {
}
Please read this for a difference between isset and array_key_exists.
The usual way is to check it like:
if(isset($_GET["hello"]) && $_GET["hello"] != ""){
//code
}
if(!empty($_GET["hello"]))
{
}
Instead of checking for both isset and $_GET != "".
Related
I have this weird error:
I have this if:
if($item->condition && $item->variable)
{
//do something
} else {
//do something else
}
Which gives me this error:
ErrorException in Controller.php line 9001: Trying to
get property of non-object
I have used dd() to debug both variables:
dd($item)->condition
if($item->condition && $item->variable)
{
//do something
}
Which returns 0 (as expected).
dd($item->variable)
if($item->condition && $item->variable)
{
//do something
}
Which returns null (also as expected). So $item is an object which contains the variable condition with 0 and the variable variable with null. However I still get this error. If I change $item->variable in the database to "foobar", dd($item->variable) will actually contain "foobar".
How come I cannot use the if here? What am I doing wrong?
you can use
if(!empty($item->condition) && !empty($item->variable))
it will check if variable isset and check for null value
use isset() function,
if(isset($item->condition) && isset($item->variable))
You can use property_exists with a check for $item to ensure that item exists.
if(!isnull($item) && property_exists($item, 'condition') && property_exists($item, 'variable'))
{
//do something
}
It would be better if you show how you have passed the $item. Try using the code in view
#foreach($item as $i)
if($i->condition && $i->variable)
{
//do something
} else {
//do something else
}
#endforeach
I want to have code like this:
If(...)
{
//do if there was a request GET or POST
}
else
{
//do if not
}
How to make this happen? Solutions like this: if($_POST['name']) let php to throw notice.
You can use PHP's isset() function, like this:
if(isset($_POST['param_name']) || isset($_GET['param_name'])) {
} else {
}
isset() determines if a variable is set and is not NULL
As #Fred suggested, We can also use PHP's empty() function. Like this:
<?php
if(!empty($_POST['param_name']) || !empty($_GET['param_name'])) {
} else {
}
empty() determines whether a variable is empty or not.
Difference between isset() and empty().
Use isset() function of php
if(isset($_POST['name']) || isset($_GET['name']))
{
//do if there was a request GET or POST
}
else
{
//do if not
}
is this the right syntax to check if a value is equivalent to NULL or not in PHP:
if($AppointmentNo==NULL)
?
I have tried it but it seems that it is not working. Any suggestion for another syntax?
Thanks
Use is_null():
if(is_null($AppointmentNo)) {
// do stuff
}
Alternatively, you could also use strict comparison (===):
if($AppointmentNo === NULL) {
An example:
$foo = NULL;
if(is_null($foo)) {
echo 'NULL';
} else {
echo 'NOT NULL';
}
Output:
NULL
There is a function called is_null.
is_null($AppointmentNo)
Use is_null().
if (is_null($AppointmentNo))
Typically you would use is_null().
if (is_null($AppountmentNo))
In the past when I needed to check if a variable was set and also a number, I would do:
if( isset($_GET['var']) && is_numeric($_GET['var']) )
But I think that's kind of ugly, especially when I need to check a bunch of variables in the same if statement, so I made a function:
function setAndNum($var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
The problem is that when I pass an undefined variable to the function, like this (supposing the variable in the GET array is undefined):
if( setAndNum($_GET['var']) )
I get the php error:
Notice: Undefined index: ...
So the whole purpose of the function is basically defeated (or half the purpose, at least ;) ).
One thing that confuses me is how the isset() function works, and why I can pass an undefined variable to it but not to my own function?
Is it possible to make my setAndNum() function work?
Your problem is with $_GET being an array. When you pass $_GET['var'] to your function, this array value is already looked up and used as an argument to the function. Therefore you cannot effectively check the presence of 'var' in $_GET from within this function. You could rewrite it a bit to make it work for array values, something like this:
function setAndNum($key, $array)
{
if(array_key_exists($key, $array) && is_numeric($array[$key]))
return 1;
else
return 0;
}
Then call it like this:
if( setAndNum('var', $_GET) )
It's good practice to verify a key exists before using it:
if (array_key_exists($_GET, 'var')) {
// do stuff with $_GET['var']
}
function setAndNum(&$var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
Please, try using this version:
function setAndNum(&$var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
You can use the # operator to prevent error reporting:
setAndNum(#$_GET['var']);
This way, the error message of the non-existant index will not be printed, and the return value will be 0.
You could also write two functions, one that checks for an array and one that checks for normal variable
function setAndNum($var)
{
if(isset($var) && is_numeric($var))
return 1;
else
return 0;
}
function setAndNumArray($array, $key)
{
if(isset($array) && isset($array[$key]) && is_numeric($array[$key]))
return 1;
else
return 0;
}
if you are using variables from GET or POST method you may do like this as these are super globals.
function setAndNum()
{
if(isset($_GET['var']) && is_numeric($_GET['var']))
return 1;
else
return 0;
}
now coming to your another query. isset checks whether a variable is s et or not like
if(isset($_POST['submit']))
{
// any code under button click
}
I want to check if a variable called $smth is blank (I mean empty space), and I also want to check if it is set using the function I defined below:
function is_blank($var){
$var = trim($var);
if( $var == '' ){
return true;
} else {
return false;
}
}
The problem is I can't find a way to check if variable $smth is set inside is_blank() function. The following code solves my problem but uses two functions:
if( !isset($smth) || is_blank($smth) ){
// code;
}
If I use an undeclared variable as an argument for a function it says:
if( is_blank($smth) ){
//code;
}
Undefined variable: smth in D:\Www\www\project\code.php on line 41
Do you have a solution for this?
Solution
This is what I came up with:
function is_blank(&$var){
if( !isset($var) ){
return true;
} else {
if( is_string($var) && trim($var) == '' ){
return true;
} else {
return false;
}
}
}
and works like a charm. Thank you very much for the idea, NikiC.
Simply pass by reference and then do isset check:
function is_blank(&$var){
return !isset($var) || trim($var) == '';
}
Whenever you use a variable outside of empty and isset it will be checked if it was set before. So your solution with isset is correct and you cant' defer the check into the is_blank function. If you only want to check if the variable is empty, use just the empty function instead. But if you want to specifically check for an empty string after a trim operation, use isset + your is_blank function.
Use empty. It checks whether the variable is either 0, empty, or not set at all.
if(empty($smth))
{
//code;
}