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).
Related
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
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
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I want to know about meaning of $$val; what is the actual meaning is?i tried to find meaning of this in google but not understand properly. Please help me in this situations.
For example: suppose i have one variable which has $$value;
meaning of $$value?
You didn't put the language, but I'll assume you mean PHP
That's a variable variable.
That means you ware asking for the value of the variable whose name.is the first variable.
Here's an example, since that's quite confusing:
$foo = "Hi";
$bar = "world";
$world = "Hello!";
echo $$bar; // "Hello!"
php fiddle: http://ideone.com/Ve4YOO
Reference: https://secure.php.net/manual/en/language.variables.variable.php
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Speed difference in using inline strings vs concatenation in php5?
Note - This was an interview question.
Given,
$a = "some text 1";
$b = "some text 2";
Which one of the following will be faster. Give some reason
$c = $a.$b;
or
$c = "$a$b";
I answered that first one will be faster because two variables are just appended. In the second case however there are variable replacements within a string. But I am not sure.
It does not matter. The speed difference will be a few microseconds. It will never have any real-world impact whatsoever.
I would say option A
For basically the same reason you stated
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