Assigning Values to Variables in PHP PHP

The equal sign (=) is used to set the value of a variable. This is called the assignment operator. On the left side of the assignment operator is a variable that will receive a value. On the right side is an expression, which could be a simple string constant or a complex combination of operators, variables, and constants.

When you assign a value to a variable, its type will change to fit the type of data you put into it. This is in contrast to C, which tries to convert values to fit the type of the variable. Assigning an integer to a variable that previously held a string converts the variable to an integer.

The simplest form of assignment is from a constant expression. This could be a numbe or a string surrounded by quotes.

Examples-of-Variables-Assignments

By now you have probably noticed showing up in most of the examples. When a appears inside a string constant surrounded by double quotes, it has special meaning: Donot print the next character. Instead the code stands for another character. This is so you can override the special meaning of certain characters, or make certain characters more visible. Strings surrounded by single quotes are treated literally. Any backslash codes are ignored, except for escaping single quotes within the string. Table 2.3 lists some backslash codes. The code stands for an end-of-line character.

Though it isn't strictly necessary, I use frequently. PHP allows you to create an entire HTML page on a single line. This is acceptable to browsers, but it's very hard to debug Core PHP Programming

your PHP script. Put a linefeed where a linefeed would appear if you were coding the page without PHP. You will spend less time picking through your output.

Related to backslash codes are embedded variables. You may write a variable inside a string surrounded by double quotes, and its value will appear in its place. This even works with arrays and objects. Listing 2.3 is an example of this technique. Notice that the RowColor variable appears within a print statement between double quotes.

Backslash-Codes

Borrowing from Perl, PHP also allows what are sometimes called "here docs". A special operator allows you to specify your own string of characters that mean the end of a string. This is helpful when you have large blocks of text that span multiple lines and contain quotes. Backslash codes and variables are recognized inside the text block, just as they are with string surrounded by double quotes. To mark an area of text, begin by using the <<< operator. Follow it by the identifier you'll use to end the string. When that identifier is found alone on a line, PHP will consider it equivalent to a closing quote character. The identifier you choose must follow the same rules governing the naming of any other identifier, as described above. It's customary to use HERE or EOD (end of data). See Listing

HERE docs

Listing-2_4


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics