Writing a Simple PHP Script

in #utopian-io7 years ago (edited)

Sessions

  1. Introduction to PHP
  2. Form Handling in PHP
  3. Using Variables and Expressions in PHP
  4. Using Variables and Expressions in PHP (Lab)
  5. PHP Operators
  6. PHP Operators (Lab)
  7. Conditional Statements in PHP
  8. Conditional Statements in PHP (Lab)
  9. Flow Control in PHP
  10. Flow Control in PHP (Lab)
  11. Functions in PHP
  12. Functions in PHP (Lab)
  13. Working with Arrays
  14. Working with Arrays (Lab)
  15. Handling Databases with PHP
  16. Working with Cookies
  17. Working with Cookies (Lab)
  18. Session Management in PHP
  19. Handling E-mail with PHP
  20. Handling E-mail with PHP (Lab)
  21. OOP Concepts
  22. 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

  1. Embed PHP scripts in the BODY tag of an HTML file

  2. Start and end every block of PHP code with tags

  3. End a PHP statement with a semicolon, ';'

  4. 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:

  1. Open the gedit text editor.

  2. Enter the code as shown in Code Snippet .

Figure displays the text editor after adding the code.

screenshotAtUploadCC_1524472407420.png

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.

  1. Save the file as date.php in the /usr/local/apache2/htdocs directory.

  2. Open the Mozilla Firefox Web browser.

  3. Enter http://localhost/date.php in the Address bar and press Enter. Figure displays
    the output.

screenshotAtUploadCC_1524472407421.png

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:

  1. Open a new file in the gedit text editor.

  2. 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

screenshotAtUploadCC_1524472407422.png

Note: The echo command is used to display text. The text to be displayed is enclosed within double quotes.

  1. Save the file as stringdisp.php in the /usr/local/apache2/htdocs directory.

  2. Open the Mozilla Firefox Web browser.

  3. Type http://localhost/stringdisp.php in the Address bar and press Enter. Figure
    displays the output of the script.

screenshotAtUploadCC_1524472407423.png

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.

  1. Open the gedit text editor.

  2. Enter the code as shown in Code Snippet

Code Snippet

<HTML>
<BODY>
<?php
$str = "My name is Samson";
echo $str;
?>
</BODY>
</HTML>

screenshotAtUploadCC_1524472407424.png

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.

  1. Save the file as, stringname.php in the /usr/local/apache2/htdocs directory.
  1. Open the Mozilla Firefox Web browser.

  2. Type http://localhost/stringname.php in the Address bar and press Enter. Figure
    displays the output.

screenshotAtUploadCC_1524472407425.png



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Submissions containing substantial instruction in ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will be rejected.

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]

Coin Marketplace

STEEM 0.24
TRX 0.21
JST 0.036
BTC 97815.63
ETH 3380.51
USDT 1.00
SBD 3.31