The following article is an excerpt from PHP & MySQL: Novice to Ninja, 7th Edition, a hands-on guide to learning all the tools, principles, and techniques needed to build a professional web application using PHP and MySQL. In this tutorial, you’ll learn the basics of PHP, including statements, variables, operators, comments, and control structures.
Once the web server has executed the PHP code embedded in a web page, the result takes the place of the PHP code in the page. All the browser sees is standard HTML code when it receives the page. That’s why PHP is called a “server-side language”: its work is done on the server.
PHP code is written in PHP tags. Like most HTML tags, PHP has a start tag and and end tag:
?> respectively. Anything inside these PHP tags is treated as PHP code and run on the server.
PHP code must be placed in a file with a
.php extension. When someone connects to the server and asks it to load a file with a
.php extension, the server then runs it as a PHP script. If you put PHP tags in a file with a
.html or any extension other than
.php, the web server won’t run any PHP code, and the PHP code will be sent directly to the browser — which doesn’t understand PHP code!
Let’s take a look at a PHP script.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Random Number</title> </head> <body> <p>Generating a random number between 1 and 10: <?php echo rand(1, 10); ?> </p> </body> </html>
To run this code, save it as
random.php in the
public directory and navigate to
Most of this is plain HTML. Only the line between
?> is PHP code.
<?php marks the start of an embedded PHP script and
?> marks its end. The web server is asked to interpret everything between these two delimiters and convert it to regular HTML code before it sends the web page to the requesting browser. If you right-click inside your browser and choose View Source (the label may be different depending on the browser you’re using) you can see that the browser is presented with something like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Random Number</title> </head> <body> <p>Generating a random number between 1 and 10: 5 </p> </body> </html>
There’s no HTML file on the server that contains this exact code. This HTML is generated dynamically on the server before being sent to the browser.
Try running the script a couple of times and notice how the number changes. PHP code has been used to generate a random number, but all signs of the PHP code have disappeared when viewing the source in the browser. In its place, the output of the script has appeared, and it looks just like standard HTML. This example demonstrates several advantages of server-side scripting:
No browser compatibility issues. PHP scripts are interpreted by the web server alone, so there’s no need to worry about whether the language features you’re using are supported by the visitor’s browser.
Access to server-side resources. The code on the server can do anything; it’s not limited to the capabilities of the browser. The resulting HTML could be generated from a database or an Excel file, or it could be the result of a calculation, such as the total of the user’s shopping cart.
Basic Syntax and Statements
A PHP script consists of a series of commands, or statements. Each statement is an instruction that must be followed by the web server before it can proceed to the next instruction. PHP statements, like those in the aforementioned languages, are always terminated by a semicolon (
This is a typical PHP statement:
echo 'This is a <strong>test</strong>!';
This is an
echo statement, which is used to generate content (usually HTML code) to send to the browser. An
echo statement simply takes the text it’s given and inserts it into the page’s HTML code at the position where the PHP script was located.
In this case, we’ve supplied a string of text to be output:
This is a <strong>test</strong>!. Notice that the string of text contains HTML tags (
</strong>), which is perfectly acceptable.
So, if we take this statement and put it into a complete web page, here’s the resulting code.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Test page</title> </head> <body> <p><?php echo 'This is a <strong>test</strong>!'; ?></p> </body> </html>
If you place this file on your web server and then request it using a web browser, your browser will receive this HTML code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Test page</title> </head> <body> <p>This is a <strong>test</strong>!</p> </body> </html>
random.php example we looked at earlier contained a slightly more complex
echo rand(1, 10);
You’ll notice that, in the first example, PHP is given some text to print directly, and in the second, PHP is given an instruction to follow. PHP tries to read anything that exists outside quotes as an instruction it must follow. Anything inside quotes is treated as text — or, to use the technical term, as a string. PHP doesn’t process strings as commands to follow. They’re treated as data in the application. You can do things with them, such as sending them to the browser, but PHP doesn’t treat one string differently from another.
So the following code will pass the string
This is a <strong>test</strong>! directly to the
echo 'This is a <strong>test</strong>!';
A string is signified using a start quote and an end quote. PHP will see the first
' as the start of the string and find the next
' and use that as the end of the string.
Anything outside of quotes is treated as a series of commands to run. The following is not valid code:
echo This is a <strong>test</strong>!;
Because the quotes have been removed, PHP will first try to run the command
This, then the command
is, followed by the command
a, and so on. As none of these are valid commands in PHP, the above code would produce an error message. If you want to treat something as text, remember to put quotes around it!
In contrast, the following code will run a valid command — the built-in “function”
rand — to generate a random number and then pass the result to the echo command:
echo rand(1, 10);
A function is a special type of command that performs a specific task. Behind the scenes, PHP will do some processing and then generate a result. In this case, the
rand function will produce a random number, but different functions perform different tasks.
You can quickly identify if something is a function because it’s followed by parentheses. PHP has many built-in functions that let you do all sorts of things, such as sending email and working with information stored in various types of databases.
PHP won’t try to run anything that’s inside a string. The following code won’t process the
echo 'rand(1, 10)';
PHP will see
'rand(1, 10)' as a string, and will send the text
rand(1, 10) to the browser, which probably isn’t what you’d want to do. It’s important to understand the difference between a string and code. PHP will see any text outside quotes as a series of commands it should follow. Anything inside quotes is a string and is data that PHP will work with.
PHP doesn’t try to understand strings. They can contain any characters in any order. But code (anything not inside quotes) — which is essentially a series of instructions — must follow a rigid structure for a computer to understand it.
Note: using a code editor with syntax highlighting makes it easy to quickly see if something is a string or code. Strings will be a different color from code that needs to be processed.
The image below shows simple code highlighting in the Visual Studio Code editor.
PHP supports both single quotes (
') and double quotes (
") to encase strings. For most purposes, they’re interchangeable. PHP developers tend to favor single quotes, because we deal with HTML code a lot, which tends to contain a lot of double quotes. For example:
echo '<a href="http://www.sitepoint.com">Click here</a>';
If double quotes were used at each end here, we’d need to tell PHP that the quote after
href= is not the end of the string by placing a
before it (known as an escape character) and do the same with any double quotes we actually wanted to send to the browser as part of the HTML:
echo "<a href="http://www.sitepoint.com">Click here</a>";
For this reason, PHP developers use single quotes, although there are some differences between single and double quotes. For our purposes here, though, they’re effectively interchangeable.
A function can be thought of as a miniature program within your program. You can instruct PHP to run the function by using its name (such as
rand in our example earlier) followed by parentheses. The instruction
rand(1, 10) tells PHP to run the function with the name
rand. Running a function is also more commonly referred to as calling a function.
Most functions in PHP return a value when they’re called. Once the value is returned, PHP behaves as if you’d actually just typed that returned value in your code instead. In the
echo rand(1, 10); example, our
echo statement contains a call to the
rand function, which returns a random number as a string of text. The
echo statement then outputs the value that was returned by the function call.
Every function in PHP can have one or more arguments that allow you to make the function behave in a slightly different way. The
rand function takes two arguments: a minimum and maximum random number. By changing the values that are passed to the function, you’re able to change the way it works. For example, if you wanted a random number between 1 and 50, you could use this code:
echo rand(1, 50);
We surround the arguments with parentheses (
(1, 50)) for two reasons. First, they indicate that
rand is a function that you want to call. Second, they mark the beginning and end of a list of arguments — PHP statements that you wish to provide — in order to tell the function what you want it to do. In the case of the
rand function, you need to provide a minimum and a maximum value. Those values are separated by a comma.
Later on, we’ll look at functions that take different kinds of arguments. We’ll also consider functions that take no arguments at all. These functions will still need the parentheses, even though nothing will be typed between them.
Source: Site Point
Republished by Blog Post Promoter