The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html> |
The concatenation operator (.) is used to put two string values together.
To concatenate two variables together, use the dot (.) operator:
<?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?> |
The concatenation operator (.) is used to put two string values together.
To concatenate two variables together, use the dot (.) operator:
<?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?> |
The output of the code above will be:
Hello World 1234 |
All variables in PHP start with a $ sign symbol.
The correct way of setting a variable in PHP:
$var_name = value; |
Let's try creating a variable with a string, and a variable with a number:
<?php $txt = "Hello World!"; $number = 16; ?> |
A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.
In PHP we have the following looping statements:
for (init; cond; incr) { code to be executed; } |
Parameters:
Note: Each of the parameters can be empty or have multiple expressions separated by commas.
You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function. The two functions are identical in every way, except how they handle errors. The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).
These two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.
Now, let's assume we have a standard menu file that should be used on all pages (include files usually have a ".php" extension). Look at the "menu.php" file below:
<html> <body> <a href="http://www.w3schools.com/default.php">Home</a> | <a href="http://www.w3schools.com/about.php">About Us</a> | <a href="http://www.w3schools.com/contact.php">Contact Us</a> |
The three files, "default.php", "about.php", and "contact.php" should all include the "menu.php" file. Here is the code in "default.php":
<?php include("menu.php"); ?> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html> |
If you look at the source code of the "default.php" in a browser, it will look something like this:
<html> <body> <a href="default.php">Home</a> | <a href="about.php">About Us</a> | <a href="contact.php">Contact Us</a> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html> |
And, of course, we would have to do the same thing for "about.php" and "contact.php". By using include files, you simply have to update the text in the "menu.php" file if you decide to rename or change the order of the links or add another web page to the site.
The PHP date() function formats a timestamp to a more readable date and time.
date(format,timestamp) |
| Parameter | Description |
|---|---|
| format | Required. Specifies the format of the timestamp |
| timestamp | Optional. Specifies a timestamp. Default is the current date and time (as a timestamp) |
The fopen() function is used to open files in PHP.
The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:
<html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html> |