difference when using & and not using & [duplicate] - php

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

Related

Arithmetic within parsed string [duplicate]

This question already has answers here:
How to increment a number inside foreach loop?
(5 answers)
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm trying to do simple addition on a variable within a string, using string parsing, like so:
$a = 2;
echo("I have {$a+1} eyes");
But PHP crashes when I do this.
I also tried {++$a}, but that outputs as I have {++2} eyes.
The PHP manual page for string parsing has no example covering arithmetic within a string.
Is this something that's possible?
UPDATE: I have to disagree with the duplicate question flag. That question is titled "How to increment a number inside foreach loop?". While the answers to that question do also answer mine, search engines will not find that question from a query like "How to increment in a PHP string", or "Arithmetic with PHP string interpolation". Even if the answers are the same, the questions are different.
It's not possible through variable interpolation. It does not accept operations as arguments. What it does accept can be read in this question.
Alternatives:
Store the result into a variable first. A bit of a redundant step, but is a way to do it with interpolation.
$a = 2;
$b = $a + 1;
echo "I have {$b} eyes";
Use concatenation. Don't forget to wrap it in parentheses, because math operators don't have precedence over concatenation.
$a = 2;
echo "I have ".($a + 1)." eyes";
Use a format printing function. Note that printf outputs the string, while sprintf returns it, so you'd have to explicitly output the latter.
$a = 2;
printf("I have %d eyes", $a + 1);
echo sprintf("I have %d eyes", $a + 1);

Facing an issue with Increment operator in PHP [duplicate]

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

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).

Which is faster - $a.$b or "$a$b" [duplicate]

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

&variablename PHP [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Can anyone explain this experssion
&variablename in PHP.
I have seen this around at many places but i am not able to figure out what does this statement do.
Thanks in advance
J
PHP reference. References in PHP are a means to access the same variable content by different names. There are three operations performed using references: assigning by reference, passing by reference, and returning by reference.
PHP reference
for example:
$example1 = 'something';
$example2 =& $example1;
echo("example 1: $example1 | example 2: $example2\n"); //example 1: something | example 2: something
$example1 = 'nothing'; //change example 1 to nothing
echo("example 1: $example1 | example 2: $example2"); //example 1: nothing | example 2: nothing
You can pass variable to function by reference, so that function could modify its arguments. The syntax is as follows:
<?php
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
// $a is 6 here
?>
taken from http://www.phpbuilder.com/manual/language.references.pass.php

Categories