Facing an issue with Increment operator in PHP [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
I am facing an unexpected issue with the increment operator in PHP. Please take a look at the given two programs:
1st Program:
<?php
$a=5;
$a++;
echo $a;
?>
it prints 6, which I clearly understood that what was happened, it just incremented the value with 1.
2nd Program:
<?php
$a=5;
$b = $a++; // just assigned incremented value to a new variable b.
echo $b;
?>
it prints 5.
Now here is the confusion, I just assigned the incremented value to the variable, so I should print 6 - why it is printing 5?

You are getting 5 because in postfix operator first it will assign the value to $b after that their value will be incremented. SO first $a is assigning to $b after that $a value will increamented

Related

Why is 1...2 equivalent to "10.2" in PHP? [duplicate]

This question already has answers here:
Why does 1...1 evaluate to 10.1? [duplicate]
(4 answers)
Closed 3 years ago.
Just a second ago I was playing around with PHP, trying to figure out if there was a native range function (eventually finding range). However, one of the things I tried was the following:
echo 1...2;
which to my surprise returns the string "10.2". Can anyone tell me exactly what syntax is responsible for this? It doesn't seem like a valid place for a splat operator.
The statement consists of three parts: 1., . and .2. The first one evaluates to the number 1, the second one is the string concatenation operator, and the latter one evaluates to 0.2. Thus, you get 10.2.
Equivalent example code:
$a = 1.;
$b = .2;
echo "a = $a\n";
echo "b = $b\n";
echo "a.b = ".($a.$b)."\n";
outputs
a = 1
b = 0.2
a.b = 10.2

PHP ${$?} Could someone shed some light? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I'm touching up on some PHP that I sometimes forget (keep the old brain going) and came across this in a PHP questionnaire. It goes as follows:
<?php
$a = "b";
$b = "a";
print ${$b} ;
//$b = "b"
?>
How does this work and how would I use it practically? Thank you in advance.
This is a variable variable
print ${$b}
It first evaluates {$b} and gets 'a'. So then it evaluates $a and gets "b" (the value stores in $a).

what does $a=[$a] mean in PHP? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
Seen in functions where $a is a function parameter:
if(!is_array($a))
$a=[$a]
I just don't know what this means,
Thanks!
It means;
if $a is not an array, then create $a as an array and use the contents (value) of $a as the first element of the newly created array called $a.
In readable English emulating a Code, this could mean:
<?php
if($a IS NOT AN ARRAY):
THEN CREATE A NEW VARIABLE $a OF TYPE: ARRAY.
TAKE WHATEVER IS INSIDE THE INITIAL $a VARIABLE...
AND PUT IT AS THE FIRST ELEMENT OF THE OVERRIDDEN, NEW $a VARIABLE.
endif;
This code convert $a from some non-array data-type to array data-type
if(!is_array($a)) \\check whether $a is not an array
$a=[$a] \\change $a to an array with only one element which was previously stored in $a
It takes a simple variable and turns it into an array.
[$a] is shorthand for array($a).
If $a is not an array, [$a] turns it into an array.

What does "++" mean in PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've been learning PHP by reading all sorts of things on the web. I've trying to figure out what this is called so I can look it up and read about it and learn how it works. It's the "++" in the code I'm assuming counts up so that the codes knows when to do a new line. I not looking for corrections to this code I'm looking for what the "++" is called so I can look it up.
$i = 0
if (++$i == 10) {
echo "new line";
}
It's called the prefix increment unary operator. It increments the number or character, then returns its value.
The postfix version does pretty much the same but returns its value first, then increments the variable.
That is a pre-increment. So in that code, $i is first incremented (increased by one), and then its value is used to test for equality to 10.
So the code that you show essentially tests if 1 equals 10 (which it does not.)
It is a number operator named the prefix increment. It takes the previous stated number and adds 1 to it. You can find more information about arithmatic operators here: Howtoforge.com
It's the pre-increment operator. It will add one to the variable it precedes, and return the result. The post-increment operator returns the current value, and then adds one. The PHP Manuel has an article on all of the operators.
All PHP Operators
Incrementing/Decrementing Operators

difference when using & and not using & [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Hi,
I was using like
$formCheckbox = $form->addElement('radio',...);
$formCheckbox->setChecked(true);
this was working on windows properly but was not on linux i.e it was not checking the radio button.
so i changed it like
$formCheckbox = **&**$form->addElement('radio',...);
$formCheckbox->setChecked(true);
so i just used & while creating the element. I just wanted to know how does it make a difference. I am using HTML quick forms.
in this case there is not much of a difference, because php handles objects internally as pointers..
but as long as you do not know what & stands for, don't use it..
a short introduction to pointer:
$a = 10;
$b = &$a;
$b = 20;
echo $a; -> 20
$a = 10;
$b = $a;
$b = 20;
echo $a; -> 10
so with & you only reference to another variable, instead of creating a new one
It is passing the variable as a reference, so that the changes are maintained always.
Check this http://php.net/manual/en/language.references.pass.php

Categories