Reference - What Does This Symbol Mean in PHP

Reference — What does this symbol mean in PHP?

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name              Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

These can go before or after the variable.

If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.

However, you must use $apples--, since first, you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c") {
echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

  • Understanding Incrementing

What is the optimum number of projects in a Visual Studio 2008 solution?

This is akin to discussions such as "how many functions should I have in a class?" and "should each enum be defined in its own .cs file?".

I would be interested to know how many classes each of your projects has. You can think of your classes, projects and solutions as organisational units. They are there to make your life easier, and to allow you (and your team) to break the overall project into managable conceptual chunks.

If VS2008 isn't complaining, and you and your developers have no problem with 50 projects in one solution then I would not worry about it.

That said, it does sound like a rather large number - but then we know nothing about the size of your overall codebase so it's hard to comment further.

What does the & sign mean in PHP?

This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:

$a = 1;

function inc(&$input)
{
$input++;
}

inc($a);

echo $a; // 2

Objects will be passed by reference automatically.

If you like to handle a copy over to a function, use

clone $object;

Then, the original object is not altered, eg:

$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1

PHP &$string - What does this mean?

You are assigning that array value by reference.

passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this copy, so if you change it in the function it won't be changed outside of it

Ref: http://www.php.net/manual/en/language.references.pass.php

What is the use of the @ symbol in PHP?

It suppresses error messages — see Error Control Operators in the PHP manual.

What does this symbol mean in PHP ?=

It's functionally the same as <?php echo $myACL->getRoleNameFromID($_GET['roleID']); ?>

What is the meaning of this symbol in PHP

$a >> $b Shift right Shift the bits of $a $b steps to the right (each step means "divide by two")

So if 5 in binary is: 101

5>>1 is 2 in binary 10

5>>2 is 1 in binary 1

This operator if common in other languages such as C.

Source



Related Topics



Leave a reply



Submit