Not redeclarable variable PHP - php

Is there any way to make an variable not redeclarable in PHP?
I mean if I set $a = 2; and then $a = 3 the value of variable $a should still be "2".

You can use define for declaring constant :
define('MY_VALUE','2');
echo MY_VALUE;//will always gives output 2
OR
echo constant("MY_VALUE");//give same output as above

Related

what is the difference between these two statements in php?

I was reading about arrow functions in php documentions and I came across this code
fn&($x = 42) => $x;
fn(&$x) => $x;
what is the difference between these two statements and what they do ?
in the second you use &$x and that's passing a variable by reference. it calls reference by value
so the $x out of the function will change if you do something in the function.
reference
here the final value of $a is 5:
$a = 5;
$y = fn ($x) => $x++;
$y($a);
echo $a;//5
but in reference by value, the final value of $a changed and it's 6:
$a = 5;
$y = fn (&$x) => $x++;
$y($a);
echo $a;//6
So you can see in reference by value, the original value of the variable can be changed
& sets a reference to the variable
fn($x = 42) : is a function with one or 0 parameter, if 0, default value 42
fn(&$x) : is a function with one parameter passed as reference, meaning if changed in the function, the new value of $x will be pass in the main.

What is the difference between $a and $$a in php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does $$ mean in PHP?
I am new to PHP and I don't know what the difference between $a and $$a is.
$a represents a variable
$$a represents a variable with the content of $a
example:
$test = "hello world";
$a = "test";
echo $$a;
output will be hello world
If $a = 'b' then $$a is $b.
This is a variable variable. They are evil. Use arrays instead (which do the same thing, but more maintainably and with the ability to use array functions on them).
$variable is a normal variable
$$variable takes the value of a variable and treats that as the name of a variable
eg:
$var = 'welcome';
echo $var //prints welcome
$$var = 'to stackoverflow';
echo "$var ${$var}"; //prints welcome to stackoverflow
echo "$var $welcome"; //prints welcome to stackoverflow
Double dollar is a powerful way to programmatically create variables and assign values them.
E.g:
<?php
$a = “amount”;
$$a =1000;
echo $amount; //echo’s 1000 on screen
?>
In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.
So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.
$a is the contents of the variable a, $$a is the contents of the variable named in $a.
Don't use this syntax in your own code.
$$a is a variable which name is in $a
Assuming $a = "foo";, $$a will be same as $foo
In PHP each variable starts with an $.
So for example you have the variable $a = 'var';
So $$a == $var
This new variable will have the "content" of the other variable as name.

Adding string with number in php

$a = "3dollars";
$b = 20;
echo $a += $b;
print($a += $b);
Result:
23
43
I have a question from this calculation.$a is a string and $b is number.I am adding both and print using echo its print 23 and print using print return 43.How is it
It casts '3dollars' as a number, getting $a = 3.
When you echo, you add 20, to $a, so it prints 23 and $a = 23.
Then, when you print, you again add 20, so now $a = 43.
The right way to add (which is technically concatenating) strings is
$a = 7;
$b = "3 dollars";
print ($a . $b); // 73 dollars
The + operator in php automatically converts string into numbers, which explains why your code carried out arimethic instead of concatenation
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatches.
To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file. Then it will show fatal error and if you didn't declare this strict then it convert string into integer.
If you need both the values, return them in an array
PHP treats '3dollars' as a integer 3 because string starting with integer and participating in arithmetic operation, so
$a = "3dollars";
$b = 20;
echo $a += $b;
it echo 23; //$a=$a+$b;
now $a = 23 + 20;
print($a += $b); //$a=$a+$b;
it print 43;
Since You have created a variable for the two, it stores the result of each, so when you added $a to 20 it will echo 23 which stores in the system, them when you print $a which is now 23 in addition to $b which is 20. You will get 43.

php variable $a contains variable $b in it update $b and $a should automatically update

I have two variables in PHP, say $a and $b. $a is a string variable. It contains $b. I want to update $a automatically if $b is updated.
$b = 4;
$a = "value is ".$b;
echo $a; // value is 4
$b = 5;
echo $a; // should print value is 5
Yes, $a can be updated automatically if you assign $b to $a by reference, but there should not be any string concatenation assigned to $a.
Try:
$b = 4;
$a = &$b;
$c = 'Value is ';
echo $c.$a;
$b = 5;
echo $c.$a;
Here is a demo
Not possible the way you want it. You see, variables can be passed by reference, like so:
$a = &$b;
Which will cause $a to automatically update when $b changes, however, it may not contain any other value, (like the string you want), so you'll have to use a function or another variable to do it.
$b = &$a;
echo "Value is $b";
or
$b = &$a;
$description = "Value is ";
echo $description . $b;
PHP doesn't have that feature. Related features you could use are:
References, which let you alias one variable to another. The value of each variable is the same, since they're simply symbol table aliases.
$b = "I'm b."
$a =& $b;
echo $a;
Variable variables, in which one variable holds the name of the other.
$b = "I'm b."
$a = 'b';
echo $$a;
However, variable variables should generally be avoided as they generally cause needless obfuscation.
Functions (as mithunsatheesh suggests). This is closest to what you want, as a function call is an expression that will have the value you're looking for. The only place a function wouldn't work where a variable would is when interpolating the value into a double-quoted string or a heredoc. Instead, you'd have to use string concatenation, or assign the result of the function call to a local variable and interpolate that.
You should pass it by reference. How to do it ?
Make a function:
function showValue(&$b)
{
return 'value is ' . $b;
}
echo showValue($b);
I think this should work.
Take a look at http://www.php.net/manual/en/language.references.whatdo.php
$a = 4;
$b =& $a;
$a = 5;
echo $b; // should print 5;
When a php script runs it runs "line after line". When you assign like this
$b = 4;
$a = "value is ".$b;
Value of $b is already assigned to $a as a integer 4 (not $b). So, if next $b is updated to some other value. Variable $a has no idea about it.
In this kind of case you have to use function or variable reference as describe in some other answers
$a = 4;
$b =& $a;
$a = 5;
echo $b;

If $a = 5, $b = 'a', what is the value of $$b?

Explain this interview question to me:
Q: If the variable $a is equal to 5 and variable $b is equal to character a, what’s the value of $$b?
A: 5, it’s a reference to existing variable.
That's a variable variable. PHP will look up the variable with the name stored in the string $b. So if $b == 'a' then $$b == $a.
It's a lot like pointers in C, except they use variable name strings instead of memory addresses to point to each other. And you can dereference as many times as you want:
$a = 5;
foreach (range('b', 'z') as $L) {
$$L = chr(ord($L) - 1);
}
echo $$$$$$$$$$$$$$$$$$$$$$$$$$z;
Output:
5
-95 is the answer as if u will echo $b u will get output as
"a"
and if u echo $a u will get out but as "5"
hence in this sense when u $(echo $b) which same as $(a) hence u will get it as "5-100" which is "-95"
$$b - 100
= $a - 100 // substituting $b=a
= 5 - 100
= -95
I don't know if the '?' is erroneous in the statement '$$b? - 100' but I don't think that will compile.
However:
$a = 5
$b = 'a';
$c = $$b - 100;
$c will equal -95, because $$b is a variable variable reference and given that $a = 5 it resolves to $a (5) - 100, or -95.
the answer is -95
$a - 100
The following is a good reference on PHP variables
http://php.net/manual/en/language.variables.variable.php

Categories