How about method of var_dump($x) in php [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
By the way i surprised about this
What will this code output and why?
$x = true and false;
var_dump($x);

Surprisingly to many, the above code will output bool(true) seeming to imply that the and operator is behaving instead as an or.
The issue here is that the = operator takes precedence over the and operator in order of operations, so the statement $x = true and false ends up being functionally equivalent to:
$x = true; // sets $x equal to true
true and false; // results in false, but has no affect on anything
This is, incidentally, a great example of why using parentheses to clearly specify your intent is generally a good practice, in any language. For example, if the above statement $x = true and false were replaced with $x = (true and false), then $x would be set to false as expected.

As explained here, the = operator has higher precedence than the and operator, causing $x = true to evaluate before true and false does, meaning that $x will take the value of true.
This will give you what you want:
$x = (true and false);

Related

Are this if statements equal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i came across a php statement and was thinking, is
if(isset($_SESSION['id'])==1){
}
very similar to
if (isset($_SESSION['id']) && $_SESSION['id']==1){
}
Let us break it down.
isset($_SESSION['id']) == 1
The first part, the isset() call, will return a boolean value (true or false), depending on if the session id is set or not.
When you compare an integer value with a boolean value, using the == operator, the integer will be coerced (or cast/type-juggled) into a boolean value. So, if the integer is a "truthy" value, i.e. greater than or equal to 1, then it will be turned into true. If it is lower than or equal to 0 then it will be turned into false.
In other words, if the session is set then the expression will be turned into true === true (which evaluates to true). If it is not set then it will be turned into false === true (which evaluates to false).
So, in conclusion, it probably does not do what you thought it did. Use the latter if statement.
The statements are not equivalent. Because isset() returns either TRUE or FALSE, the first test:
if(isset($_SESSION['id'])==1){
}
is exactly equivalent to:
if(isset($_SESSION['id'])){
}
because php converts 1 to the Boolean value TRUE so the == operator has compatible types. The second, on the other hand, not only test whether $_SESSION['id'] is set, but also, if it is, whether the value is set to 1 (after type juggling to resolve comparing a string with an integer).

Assignment in PHP with bool expression: strange behaviour [duplicate]

This question already has answers here:
'AND' vs '&&' as operator
(10 answers)
Closed 7 years ago.
Why is $x true in this statment?
$x = true and false;
I've got the problem with some variables but I could reduce the problem to the primitive boolean values.
Update:
As you see in the replies the effect has to do with the operator precedense in PHP. There is also a good explanation about the problem in this question, which I couldn't find in the net before since I didn't know that I have a problem with this and I didn't know that there is a difference between '&&'/'||' and 'and'/'or'.
After some search I found out that this is caused by the operator precedence in PHP. The '=' operator is stronger than 'and'.
If you want to get the expected result you have to use braces:
$x = (true and false); //correct: $x is false now!
Without braces the expression is equal to ($x = true) and false; . $x will get the 'true' value. After that the PHP interpreter 'replaces' this assignment with the value that $x has just got. So true and false; remains. And that does not do anything. 'false' is lost and didn't influence the expression.
Be aware that the braces are NOT required if you use '&&' or '||'! They are stronger than '=' and thus stronger than 'and' and 'or'. Strange...
This is different to e.g. $x = 5 + 6; since here '+' is stronger than '=' it will be resolved first. In the end it affects only the boolean operators 'and', 'or' and 'xor'. Only with them you have to watch out.
Note also that this is different compared to other languages like JavaScript or Java. In those languages you don't need the braces since the logical operators are stronger than the equal operator (Operators in JavaScript, Operators in Java).
More on this:
$x = true and false;
So we have the following. If you guessed that $x is true, you'd be right. Since = has a higher precedent than and.
Rewriting the expression above it would be equal to this:
($x = true) and false;
Here's where it gets funny.
If you were to do:
$x = true && false;
and you'd guess that $x is true, you'd be wrong. $x is actually false in this case.
The above expression is actually equal to:
$x = (true and false);
Why? Because && has a higher precedent than and.

What is the difference between if(isset($a)) and if($a) in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What is the difference between if(isset($a)) and if($a) or if_exist($a) and if($a) in php?
With $a = false;:
if ($a) {} will return false, whereas if (isset($a)) {} will return true.
I do not know that if_exist you speak of. :)
Edit: Please check #Utkanos's answer for an excellent and more expansive explanation. :)
if (isset($var))
checks that a variable has been set and that it has a non-null value.
if ($var)
assumes the variable has been set and checks instead for it having a truthy value.
PHP has no function if_exist. Perhaps you're thinking of array_key_exists, which returns true if an array contains a certain key.
isset($var) checks wether the variable $var is not of type null and returns a boolean true or false.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
if($var) does an implicit type conversion to convert the $var from its original type to boolean which means it checks for truthy or falsy values.
To check wether a variable is truthy you can explicit convert it to a boolean:
$var = "foo";
var_dump((bool)$var); // outputs 'bool(true)'
$var = "0";
var_dump((bool)$var); // outputs 'bool(false)'
For a full list of how PHP handles the diffrent variable types see:
http://www.php.net/manual/en/types.comparisons.php

Does PHP load the code inside a isset even if its not set? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a performance question.
I structured a page in 4 different isset($_GET[""])
Lets call them
Menu1
Menu2
Menu3
Menu4
My question is, when the page is set to, example Menu1 (index.php?Menu1), is PHP still loading the code for Menu2, 3 and 4 even though they aren't set?
If it does I guess this is a very bad structure that will slow down the website if it gets alot of traffic since in some of those Menu's I use mysql.
Thanks in advance!
If you say if(isset($stuff)), the code inside the conditional will only be run if $stuff is set
If you have if(isset($_GET['menu1']), and go to index.php?menu1=something,
All the code inside other if's will never be run. Only the conditions would be checked.
The isset will only evaluate variables, no functions nor function return values are allowed. From the PHP Manual (isset):
<?php
$a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple'));
var_dump(isset($a['test'])); // TRUE
var_dump(isset($a['foo'])); // FALSE
var_dump(isset($a['hello'])); // FALSE
// The key 'hello' equals NULL so is considered unset
// If you want to check for NULL key values then try:
var_dump(array_key_exists('hello', $a)); // TRUE
// Checking deeper array values
var_dump(isset($a['pie']['a'])); // TRUE
var_dump(isset($a['pie']['b'])); // FALSE
var_dump(isset($a['cake']['a']['b'])); // FALSE
If you put multiple conditions in an if statement using the AND operator, the if statement will stop evaluating as soon as it find the first false condition. In the following example foo() will never be called:
$a = (false && foo());
$c = (false and foo());
In a similar way you can give the isset multiple parameters to evaluate. The manual says "If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered." :
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE
unset ($a);
var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE

Ternary Operator not working in godaddy php (parsererror) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
<br />↵<b>Parse error</b>: syntax error, unexpected ':' in <b>.../ajax.php</b> on line <b>87</b>
LINE 87: $conditions = ($this->input->post()) ? : array('tutor'=>$this->session->userdata('user_id'));
line 87 works fine on localhost, but when I use godaddy I get that error. Is there something I need to set in php.ini or something to get Ternary operators to work?
Thanks!
The ternary operator (as the name suggests) usually expects 3 arguments
$var = $expr ? $trueValue : $falseValue;
With PHP5.3 its allowed to omit $trueValue. In this case its $expr is used for it
$var = $expr ? : $falseValue;
// same as
$var = $expr ? $expr : $falseValue;
You probably don't have PHP5.3 on your server. As you can see in my example its quite easy to fix this and make it ready for pre-5.3
$conditions = ($this->input->post())
? ($this->input->post())
: array('tutor'=>$this->session->userdata('user_id'));
The syntax ? : you are using is PHP 5.3 only.
Set a default value:
$conditions = ($this->input->post()) ? $this->input->post() : array('tutor'=>$this->session->userdata('user_id'));
The accepted answer is correct, but misses one important point:
$x = $a ? : $b; // valid in PHP 5.3
should indeed be replaced with
$x = $a ? $a : $b; // valid in older versions of PHP
However, when you're dealing with functions, not variables, be aware of side-effects:
$conditions = ($this->input->post())
? ($this->input->post())
: array('tutor'=>$this->session->userdata('user_id'));
In the above case, if $this->input->post()returns a truthy value, the function will be executed again, which may not be what you want. You can see this more clearly by expanding the ternary operator to its full form:
if ($this->input->post()) {
$conditions = $this->input->post();
} else {
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
You can see that the function is executed on lines one and two above. If you don't want that, try this instead:
if (!$conditions = $this->input->post()) {
// Single equal sign in an if condition: make assignment, and check
// whether the result is truthy.
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
This is a fairly common pattern in my own code. This will execute the function $this->input->post() only once. If the result is truthy, that result is stored in $conditions. If the result is not truthy, the code inside the if condition is run. This assigns the fallback value to $conditions. The benefit is that, in either case, $this->input->post() is run only once.

Categories