PHP & MySQL Tutorial
- Variables
In this chapter, we'll briefly look at the basic components of PHP such as echo, variables, and data types.
<html> <?php echo '<h1>Rodion Romanovich Raskolnikov</h1>' ; ?> </html>
Notice the semi-colon at the end of the PHP script. This terminator should appear at the end of each PHP statement.
Comments can be added inside the PHP and the PHP parser treats any text between "//" and the end of that line as a single-line comment. Or the single-line comment may begin with "#" character. As in C, we can put block of comments with "/*" and "*/".
<html> <?php /* This is a multi-line comment */ echo '<h1>Rodion Romanovich Raskolnikov</h1>' ; // Single-line comment starts with "//" # Another single-line comment starts with "#" character ?> </html>
When the code is displayed in a web browser, the browser's View->Source shows only this code:
The backslash ("\") can be used as an escape for the character that immediately follows it. This is useful to include quotation marks within a test to be written with the echo. As the string is itself enclosed by quotes, the PHP parser would be confused by additional quotes. The escape character can save us. This can be easily avoided by preceding the additional quotes by a backslash to escape them.
Let's look into the following code:
<html> <?php echo "<textarea rows=\"5\" cols=\"60\">"; echo "\"Program testing can be used to show the presence of bugs,\n"; echo " but never to show their absence!\""; echo "\n\n \t\t\t Edsger Dijkstra (1931 - 2006)"; echo "</textarea>"; ?> </html>
All variables start with a $ character, followed by the name of a variable. Data is assigned to a variable using the "=" operator and the assignment statement should end with ";".
The following example is practically the same as the previous one except the entire text contents is assigned to a variable call $quote, which makes this example more efficient than the previous example because it uses a single call rather than five calls.
<html> <?php $quote = "<textarea rows=\"5\" cols=\"70\"> \"Program testing can be used to show the presence of bugs, but never to show their absence!\" \n \t\t\t Edsger Dijkstra (1931 - 2006) </textarea>"; echo $quote; ?> </html>
Output from the script is the same as before.
Notice that the $quote variable is not surrounded by quotes in:
echo $quote;
because it is not to be treated literally.
PHP is a loosely typed language, so its variables can store different types of data. This is different from other programming languages such as C++ and Java where we must declare a variable to be of a specific data type and may then store only data of the declared type.
PHP supports the following basic data types:
- Integer Used for whole numbers
- Float (double) Used for real numbers
- String Used for string of characters
- Boolean Used for true or false values
- Array Used to store multiple data items
- Object Used to store instances of classes
Tow special types are:
- NULL Variables that have not been given a value, have been unset, or have been given the specific value NULL are of type NULL
- resource Certain built-in functions such as database functions return variables that have the type resource. They represent external resources such as database connections.
Here is an example code:
<html> <?php $str = "I am a string"; $i = 2014; $f = 3.14159; $nl = NULL; echo "String: $str <br />"; echo "Integer: $i <br />"; echo "Float: $f <br />"; echo "Null: $nl"; ?> </html>
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization