Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Variable-assignment in PHP

Sunday, July 27th, 2014 by Servage

php-variableThe syntax to assign a value to a variable is always variable = value. Or, to reassign the value to another variable, it is other_variable = variable.

There are also a couple of other assignment operators that you will find useful. For example, we’ve already seen:

$x += 10;

which tells the PHP parser to add the value on the right (in this instance, the value 10) to the variable $x. Likewise, we could subtract as follows:

$y -= 10;

Variable incrementing and decrementing Adding or subtracting 1 is such a common operation that PHP provides special operators for these tasks. You can use one of the following in place of the += and -= operators:

++$x;
−−$y;

In conjunction with a test (an if statement), you could use the following code:

if (++$x == 10) echo $x;

This tells PHP to first increment the value of $x and then test whether it has the value 10 and, if so, output its value. You can also require PHP to increment (or, in the following example, decrement) a variable after it has tested the value, like this:

if ($y−− == 0) echo $y;

which gives a subtly different result. Suppose $y starts out as 0 before the statement is executed. The comparison will return a TRUE result, but $y will be set to −1 after the comparison is made. So what will the echo statement display: 0 or −1? Try to guess, and then try out the statement in a PHP processor to confirm. Because this combination of statements is confusing, it should be taken as just an educational example and not as a guide to good programming style.

In short, whether a variable is incremented or decremented before or after testing depends on whether the increment or decrement operator is placed before or after the variable.

By the way, the correct answer to the previous question is that the echo statement will display the result −1, because $y was decremented right after it was accessed in the if statement, and before the echo statement.

String concatenation

String concatenation uses the period (.) operator to append one string of characters to another. The simplest way to do this is as follows:

echo "You have " . $msgs . " messages.";

Assuming that the variable $msgs is set to the value 5, the output from this line of code will be:

You have 5 messages.

Just as you can add a value to a numeric variable with the += operator, you can append one string to another using .= like this:

$bulletin .= $newsflash;

In this case, if $bulletin contains a news bulletin and $newsflash has a news flash, the command appends the news flash to the news bulletin so that $bulletin now comprises both strings of text.

String types

PHP supports two types of strings that are denoted by the type of quotation mark that you use. If you wish to assign a literal string, preserving the exact contents, you should use the single quotation mark (apostrophe), like this:

$info = ‘Preface variables with a $ like this: $variable';

In this case, every character within the single-quoted string is assigned to $info. If you had used double quotes, PHP would have attempted to evaluate $variable as a variable.

Sources for further reading

Variable-assignment in PHP, 4.0 out of 5 based on 1 rating
Categories: Guides & Tutorials, Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.