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
Related
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
}
I have the following script:
function canLogin($mail, $password) {
if( isset($mail, $password) ) {
$login = dbFind('users', array("mail" => (string)"$mail", "pass" => (string)"$password"), true);
var_dump($login);
if($login)
{ return (string)$login['_id']; }
else { return false; }
} else { return false; }
}
which function dbFind is the following:
function dbFind($collIn, $obj, $one = false) {
global $db;
$collection = $db->$collIn;
if($one == true)
return $collection->findOne($obj);
else return $collection->find($obj);
}
now, as you can notice from the first snippet of code, I am using var_dump to see the content of the $login variable and afterwards check if it is set or not.
When I have a right login, the var_dump returns the correct object from the db, otherwise it returns NULL. The problem is that in the if statement just after the var_dump always returns false!
What I am missing? Thanks in advance.
If (isset($login)){
//do what you need
}
if($login != NULL){
return (string)$login['_id'];
}else{
return false;
}
I had such an issue and this worked and it's working.
Try changing the if statement to something like:
if (is_array($login) && array_key_exists('_id', $login) && $login['_id'] instanceof MongoId)
{
//the array_key_exists check could be omitted, but is there just in case
return (string) $login['_id'];
}
You can omit the first two checks and use !empty($login['_id']) && $login['_id'] instanceof MongoId, but that might issue warnings (not sure).BTW: try setting your ini file to E_STRICT | E_ALL, maybe that'll give you some more clues as to what's going wrong.
From what you've posted in your comment, and looking at your code, this line: return (string)$login['_id']; is actually casting an object to a string. The result looks like this:
array(5) { ["_id"]=> object(MongoId)#8 (1) //<-- OBJECT
{ ["$id"]=> string(24) "50d19fed9cc2318521000001" }
}
And you're using the (string) cast, that just doesn't feel right As Sammaye pointed out, that should work. You're casting too much BTW: "$foo" casts $foo to a string, because of the double quotes, no need for an extra cast... but that's not the point... Anyway, try this:
return $login['id']->{'$id'};//need the single quotes, the property NAME is $id, not id
See the docs on the mongoid class for more details on the methods/properties, and headaches...
PS: it might be cleaner to use the getter method:
$login['_id']->getPID();//returns an int, not a string, though
Good luck, happy coding
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
}
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 != "".
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;
}