What Does PHP Keyword 'Var' Do

What does PHP keyword 'var' do?

It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICT warning in PHP from version 5.0.0 up to version 5.1.2, as of when it was deprecated. Since PHP 5.3, var has been un-deprecated and is a synonym for 'public'.

Example usage:

class foo {
var $x = 'y'; // or you can use public like...
public $x = 'y'; //this is also a class member variables.
function bar() {
}
}

What is meant by $$$var. IS it works in php

It is Variable variables. This will make you understand:

<?php
$a = 123;
$b = 'a';
$var = 'b';
echo $var; //b
echo $$var; //a
echo $$$var; //123

Should I be using the var keyword in PHP?

var is identical to public. BUT most modern coding standards prefer 'public' over var.

Howevever, there is no real difference at all.

What does var or var equals to mean in php?

tldr; This is basically just an initialization step and ensures the variable $var holds some value that is not falsey. In English it is saying the following:

Evaluate the value stored in $var. If it is truthy, great stop right there. Otherwise, set $var equal to $something.

How it works

This takes advantage of the short-circuiting nature of OR, as mentioned in the manual:

// foo() will never get called as those operators are short-circuit

$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());

While I find this is common in JavaScript, I rarely see this type of code in PHP. I believe this has to do with the fact that, in JavaScript, you can call a function without having to pass any of the named parameters in:

function testFn(param1, param2) {
param1 = param1 || 'default value';
param2 = param2 || 'default param2 value';
}
testFn();
testFn(12);
testFn(12, 13);

All three of those function calls are legal. The first one will set the parameters to their 'default ...' values. The second one will set the first parameter to 12 and the second one to 'default param2 value', and the third one will set the first parameter to 12 and the second parameter to 13.

In PHP you can also, but you have to supply default values for the variables in the function signature or it will result in a PHP Warning: Missing argument 1 for ...:

function testFn($param1 = 'default1', $param2 = 'default2') { }
testFn();
testFn(12);
testFn(12, 13);

Setting the default parameters via the function's signature to me seems a much more readable way of doing it.

Long-winded Concrete Example

Consider this statement:

if(false && functionThatReturnsTrue()) {}

Clearly this cannot ever be true overall, but the important point here is that functionThatReturnsTrue never gets called. Now, I can pull out that entire conditional to make a brand-new, completely legal statement:

false && functionThatReturnsTrue();

Still, functionThatReturnsTrue is not getting called. If I change the && to an ||, though, it will:

function functionThatReturnsTrue() {
echo "I'm true!";
}

false || functionThatReturnsTrue();

Above, notice that functionThatReturnsTrue doesn't even return anything! the entire expression above will be evaluated (it will evaluate overall to false), but will not be stored anywhere. Also note that the function will be called and "I'm true!" will be displayed on the screen.

Now let's replace functionThatReturnsTrue with a variable assignment:

false || $var = 12;

Here, there will be three expressions that will be evaluated: false (which has no side-effects), $var = 12 (which will set $var equal to 12), and the overall expression false || 12 (because the result of assigning a value to a variable is the value being assigned, which is why you can also do things like return $var = 12;). After that line of code executes, $var will be equal to 12.

Let's now replace that first false with your $var:

$var || $var = 12;

If $var happens to be anything other than null, false, 0, or '' (anything that can evaluate to false), then the expression $var = 12 will not be executed, and $var will have the same value it had before this line ran. If it is falsey, however, then it will no longer equal whatever it was, it will now equal 12.

Let's take one more step and make it just a tad bit more complicated:

$whatAmI = ($var || $var = 12);

Here, I took the previous expression, wrapped it in parentheses, and I am actually keeping it's result inside a variable named $whatAmI rather than just throwing it into the bit bucket. $whatAmI will be a boolean value (because it is holding the result of the || operation) and there could have been two side effects to this line being executed:

  1. $var may have been updated to the value 12
  2. $whatAmI will now be holding a boolean value of true

PHP what does it mean when using this '$$var'?

You're looking at a variable variable. e.g.

// original variable named 'foo'
$foo = "bar";

// reference $foo dynamically by evaluating $x
$x = "foo";
echo $$x; // "bar";
echo ${$x}; // "bar" as well but the {} allows you to perform concatenation

// different version of {} to show a more "complex" operation
$y = "fo";
$z = "o";
echo ${$y . $z}; // "bar" also ("fo" . "o" = "foo")

To show an example more closely matching your question:

$foo = "foo";
$bar = "bar";
$baz = "baz";

$ary = array('foo' => 'FOO','bar' => 'BAR','baz' => 'BAZ');
foreach ($ary as $key => $value){
$$key = $value;
}

// end result is:
// $foo = "FOO";
// $bar = "BAR";
// $baz = "BAZ";

PHP, $this->{$var} -- what does that mean?

This is a variable variable, such that you will end up with $this->{value-of-$val}.

See: http://php.net/manual/en/language.variables.variable.php

So for example:

$this->a = "hello";
$this->b = "hi";
$this->val = "howdy";

$val = "a";
echo $this->{$val}; // outputs "hello"

$val = "b";
echo $this->{$val}; // outputs "hi"

echo $this->val; // outputs "howdy"

echo $this->{"val"}; // also outputs "howdy"

Working example: http://3v4l.org/QNds9

This of course is working within a class context. You can use variable variables in a local context just as easily like this:

$a = "hello";
$b = "hi";

$val = "a";
echo $$val; // outputs "hello"

$val = "b";
echo $$val; // outputs "hi"

Working example: http://3v4l.org/n16sk

What does ${/*var in here*/} do. I cannot find any results anywhere

It's a variable variable. The resultant string inside the braces is used as the name of the variable. So if you have the following:

$arr = array('foo' => 'bar');

foreach( $arr as $key => $val ) {
${$key.'Count'} = 0;
}

There will now be a variable $fooCount, with the value 0.



Related Topics



Leave a reply



Submit