What is PHP document?
PHP document is a text file like HTML file which contains PHP scripts and it is stored on the server. In other words PHP documents that contains PHP scripts that executed on server and the result is parsed by HTML to display on web browsers.
Example - 1
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$x = 'https://www.progrramers.com';
$y = 'PHP Tutorials';
echo '<a href = '; ?> <?php echo $x . '>';?> <?php echo $y.'</a>';?>
</body>
</html>
Paste above code into your text editor, save as “anyname.php” and try to run the php script over http localhost it will show a link “PHP Tutorials”. Right click on browser’s blank area than click on view page source. You will find only HTML code not PHP script. It means that PHP script executed on the server and HTML display the results.
Example - 2
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$x = 'https://www.progrramers.com';
echo '<a href = "https://www.progrramers.com">' .$x. '</a>';
?>
</body>
</html>
Same thing can be scripted in other way in above example but the result is same. It might be enough to make you understand that How php works actually. Let’s move forward
PHP file Extension
Like it is mentioned above PHP documents are save with “.php” extension. Although the php scripts are written along with HTML, CSS or JS codes, to execute the php script you must save the documents with “.php” extension. It doesn’t affects HTML, CSS or JS code execution.
Example - 3
<!DOCTYPE html>
<html lang = "en-US">
<head>
<style>
.php-main{
background:#333333;
color:#CCCCCC;
margin: 10%;
padding: 10%;
text-align:center;
border-radius: 15px;
box-shadow: 5px 5px 5px;
display:none;
}
.php-ver{
text-shadow:2px 2px 2px black;
}
</style>
<script>
function showPhp(){
var txt = document.getElementsByClassName('php-main');
var btn = document.getElementsByClassName('btn');
txt[0].style.display = 'block';
btn[0].style.display = 'none';
}
</script>
</head>
<body>
<div style = "background:#333333; color:#CCCCCC; margin: 2%; padding: 2%; text-align:center;border-radius: 15px;">
<h1>PHP Tutorials</h1>
</div>
<div class = "php-main" id = "php-main">
<?php
echo '<h2>Current Version of PHP is <br>'.'<span class = "php-ver">'.phpversion().'</span>'.'</h2>';
?>
</div>
<button class = "btn" onclick = "showPhp()">Show PHP Version</button><br /><br />
</body>
</html>
Basic PHP Syntax
PHP scripts are written inside php tags “<?php ?>”. PHP tags placed anywhere on the php document. If you are writing php script in the standard html document then php scripts can be written before <!DOCTYPE html> declaration.
Tips: To create Session and Cookies php scripts can be written top of the HTML codes before doctype declaration. We will study about Session and Cookies later chapters.
Example - 4
<?php
//Statement goes here...
?>
PHP Expressions and Statements
Inside the php tags expressions and Statements are defined. A statement in PHP is any expression that is followed by a semicolon (;). In above examples you can see that each expressions that forms the Statement is ended with semicolon (;). See below example also
Example - 5
<?php
$x = "Hello World";
echo $x;
?>
PHP Comments
If you have been through “HTML5 Tutorial - HTML Comments” you are already aware about the comments that is used while developing a web site. A comment is the portion of a program that exists only for the human reader and stripped out before displaying the programs result. There are two commenting formats in PHP
1. Single Line Comments
2. Multi Line Comments
Below are the examples of both single line and Multi line comments
Example - 6
<?php
// This is an example of Single Line Comment.
# This is an another example of Single Line Comment
/*
This is an example of Multi Line Comment
This is Second Line.
This is Third Line.
*/
$x = "Hello World";
echo $x;
?>
In next chapter we will study PHP Case sensitive and Whitespace Insensitive
Post A Comment:
0 comments: