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, hence the name “server-side language.” Let’s look at this today.php example:
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Today’s Date</title></head><body><p>Today’s date (according to this web server) is<?phpecho date('l, F jS Y.');?></p></body></html>
Most of this is plain HTML, except the line between <?php and ?> 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. The
browser is presented with the following:
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Today’s Date</title></head><body><p>Today’s date (according to this web server) isSunday, April 1st 2012.</p></body></html>
Notice that all signs of the PHP code have disappeared. 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
Reduced load on the client
Basic Syntax and Statements
C++, C#, Objective-C, Java, Perl, or any other C-derived language. But if these languages are unfamiliar to you, or if you’re new to programming in general, there’s no need to worry about it.
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 of the PHP script where it was contained.
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> and </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>Today’s Date</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>Today’s Date</title></head><body><p>This is a <strong>test</strong>!</p></body></html>
The today.php example we looked at earlier contained a slightly more complex echo statement:
echo date('l, F jS Y.');
Instead of giving echo a simple string of text to output, this statement invokes a built-in function called date and passes it a string of text: 'l, F jS Y.'. You can think of built-in functions as tasks that PHP knows how to do without you needing to spell out the details. PHP has many built-in functions that let you do everything, from sending email to working with information stored in various types of databases.
When you invoke a function in PHP – that is, ask it to do its job – you’re said to be calling that function. Most functions return a value when they’re called; PHP then behaves as if you’d actually just typed that returned value instead in your code. In this case, our echo statement contains a call to the date function, which returns the current date as a string of text (the format of which is specified by the text string in the function call). The echo statement therefore outputs the value returned by the function call.
You may wonder why we need to surround the string of text with both parentheses ((…)) and single quotes ('…'). As in SQL, quotes are used in PHP to mark the beginning and end of strings of text, so it makes sense for them to be there. The parentheses serve two purposes. First, they indicate that date is a function that you want to call. Second, they mark the beginning and end of a list of arguments that you wish to provide, in order to tell the function what you want it to do.
In the case of the date function, you need to provide a string of text that describes the format in which you want the date to appear. Later on, we’ll look at functions that take more than one argument, and we’ll separate those arguments with commas. We’ll also consider functions that take no arguments at all. These functions will still need the parentheses, even though there will be nothing to type between them.
There are loads more practical and hands-on examples just like this in the 500+ pages of the book, which covers tutorials, installation, PHP coding, database design, Object Oriented Programming (OOP), building a CMS, shopping carts and latest technologies.
If you're interested in the book, you can: