Sessions
- Introduction to PHP
- Form Handling in PHP
- Using Variables and Expressions in PHP
- Using Variables and Expressions in PHP (Lab)
- PHP Operators
- PHP Operators (Lab)
- Conditional Statements in PHP
- Conditional Statements in PHP (Lab)
- Flow Control in PHP
- Flow Control in PHP (Lab)
- Functions in PHP
- Functions in PHP (Lab)
- Working with Arrays
- Working with Arrays (Lab)
- Handling Databases with PHP
- Working with Cookies
- Working with Cookies (Lab)
- Session Management in PHP
- Handling E-mail with PHP
- Handling E-mail with PHP (Lab)
- OOP Concepts
- OOP Concepts (Lab)
Writing a Simple PHP Script
Writing a PHP script is similar to writing an HTML script. A PHP file can include simple text, HTML tags,
and PHP script.
The rules to be followed while creating a PHP script are as follows
Embed PHP scripts in the BODY tag of an HTML file
Start and end every block of PHP code with tags
End a PHP statement with a semicolon, ';'
Save all PHP files with a .php extension
Note: The .php extension enables the Web server to process the file as a PHP file
Code Snippet displays a simple PHP script.
<html>
<body>
<title>PHP Syntax Example</title>
<?php
echo "Hello World";
?>
</body>
</html>
The echo command in PHP is used to send data to the browser. This command is used to print data
on a browser. The instructions for the echo command are included within the php tags
and ends with a semicolon. The file is saved with a .php extension and will display "Hello World" on the
browser when executed.
Comments in a PHP Script
Comments in a code assist a programmer to interpret the meaning of a code. They are not displayed
in the output and are meant only for the programmers. PHP supports both single-line and multi-line
comments.
Code Snippet illustrates the use of comments in a PHP script.
<?php
// This is a single-line comment
/* and this is a
multi-line
comment */
?>
Some simple PHP scripts are listed as follows:
To display current date using the PHP script:
Open the gedit text editor.
Enter the code as shown in Code Snippet .
Figure displays the text editor after adding the code.
In figure , the PHP script is included within the BODY tag of the HTML code. A code line
must end with a semicolon to separate one instruction from another.
The PHP script engine processes the text enclosed within tags.
After processing the PHP script, the contents are returned to the browser as a normal Web
page.
Save the file as date.php in the /usr/local/apache2/htdocs directory.
Open the Mozilla Firefox Web browser.
Enter http://localhost/date.php in the Address bar and press Enter. Figure displays
the output.
Figure : Displaying Current Date
In the PHP script, an echo command is used. The echo command displays the output in a Web
browser. The gmdate() is a PHP function that enables to display the current date and time in
the browser. The letters gm stand for Greenwich Mean Time. The letter, M, used in the gmdate()
function displays only first three letters of the month in the current date. The letter d displays the
current date and Y displays all the four digits of the current year.
To display a simple text in the browser using the PHP script:
Open a new file in the gedit text editor.
Enter the code as shown in Code Snippet
Code Snippet
<HTML>
<BODY>
<?php echo "Hello Everybody";
?>
</BODY>
</HTML>
displays the text editor after adding the code
Note: The echo command is used to display text. The text to be displayed is enclosed within double quotes.
Save the file as stringdisp.php in the /usr/local/apache2/htdocs directory.
Open the Mozilla Firefox Web browser.
Type http://localhost/stringdisp.php in the Address bar and press Enter. Figure
displays the output of the script.
To display text in the Web browser with the help of a variable:
The rules to be followed while using a variable in a PHP script are as follows:
PHP variables must start with a dollar sign '$'
PHP variables can contain strings, numbers, and arrays
Variable names must start with a letter or an underscore ' _ '
Variable names can only contain alpha-numeric characters and underscores and no spaces.
Note: The data type for a variable in PHP need not be specified while declaring the variable. PHP
automatically assigns the correct data type for a variable depending upon the value assigned to the
variable.
The following example shows how to display text using a variable.
Open the gedit text editor.
Enter the code as shown in Code Snippet
Code Snippet
<HTML>
<BODY>
<?php
$str = "My name is Samson";
echo $str;
?>
</BODY>
</HTML>
In figure , the text string is assigned to a variable named str. The second instruction substitutes
the value or content of the variable str to the echo command. The echo command displays the
contents of the str variable in the output.
- Save the file as, stringname.php in the /usr/local/apache2/htdocs directory.
Open the Mozilla Firefox Web browser.
Type http://localhost/stringname.php in the Address bar and press Enter. Figure
displays the output.
Posted on Utopian.io - Rewarding Open Source Contributors
Unfortunately, in accordance to this rule. I have to reject this contribution. To see which kind of contribution we accept; You can check our Tutorials section.
Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.
[utopian-moderator]
Hey @deathwing, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Rejected for similar reason to prior contribution by same user. Plagiarism.
Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.
[utopian-moderator]