Welcome to PHPBash | Helping You to Become an Expert in PHP Programming. In this PHP Tutorial for beginners and professionals, we will learn details about Precious Tips To Help You Get Better At Basics of PHP Syntax, Including PHP script in HTML and Executing PHP Script. As well PHP case sensitivity, PHP tags and their types, Comments in PHP, Whitespace in PHP & PHP Block Concept.
Basics of Php Syntax
This PHP tutorial covers all the topics of Basics of PHP Syntax like
1. Basic PHP Syntax
2. PHP Case Sensitivity
3. Types of PHP Tags
4. Comments in PHP
5. White Spaces in PHP
6. Blocks in PHP
7. PHP Expressions and Statements
8. Executing/Running PHP Script in Windows Command Prompt(cmd)
Now let's start this tutorial with understanding Basic PHP Syntax.
Basic PHP Syntax
PHP is very popular and most widely used and the reason is that,PHP is simple to learn and use. The code of PHP is executed on server and after execution, the result return to the browser in plain HTML language.
Basically, Every PHP statement is included in PHP tags <?php and ?>. It is Called Escaping to PHP. The PHP file is Saved with extension .php in the Root directory of web server. You can check more details in previous tutorial about PHP Programming Fundamentals - Introduction to PHP. Lets understand following simple Syntax followed by Example of printing Hello World -
Syntax:
<?php
// code statements
?>
Example:
<?php
echo "Hello World";
?>
To Execute above script, put http://localhost/hello.php in browser and hit enter.The output of program will be Hello World & it will displayed in browser.
Now, another way to create php file is embed or include php tags in html. You may have any number of php blocks in html. Generally it should be placed in <body> tag of html.The following example will demonstrate embedding php tags in html.
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
In above example, the PHP tags <?php and ?> are placed in body part of HTML page. This file should be saved with extension .php. In PHP, for printing or displaying text on screen, echo statement is used. so the output of program will be Hello World & it will displayed in browser.
PHP Case Sensitivity
PHP is Case Sensitive language means that, $PHPBash and $PHPBASH is different variable in PHP. In PHP, variable names are case sensitive. On other hand, regarding predefined keywords like while,for,if,switch,predefined functions, user defined functions, class names are incasesensitive in PHP.
For Example:
<?php
echo "PHP Case Sensitivity Test";
ECHO "PHP Case Sensitivity Test";
?>
Both statements in above example will produce same result.
Look at another example:
<?php
$count = 1;
echo "Count is " . $count. "<br>";
echo "Count is " . $COUNT. "<br>";
?>
In above example, only statement will print value of $count. The second statement will not print anything because $count and $COUNT are treated as different in PHP.
Types of PHP Tags
As described earlier, each PHP script statement must be enclosed with <?php and ?>. Basically, when we try to execute PHP script, PHP parser is searching for opening and closing tags. so all statements within <?php and ?> are interpreted by PHP parser regardless of where they are embedded.
<?php
// code statements
?>
Because of this, PHP can be embedded in various forms of files. PHP parser simply ignores everything outside of <?php and ?>.
Another way to use PHP tags is Short-open tags. They look as <?php ... ?>.
Comments in PHP
In programming languages, comments are basically not executed by compiler or interpreter. In PHP as well, we can use comments for making code more readable and comments help to describe the code functionalities, logic and what particular piece of code is doing.
Comments are also useful for providing more details about particular statement or function or any other statement. Like other programming language PHP also supports two types of Comments- Single Line Comments and Multi-line Comments.
For Single Line Comments, you need to use // or # before the comment line. Consider following example,
<?php
# Single Line Comment 1
# Single Line Comment 2
# Single Line Comment 3
// Another way to use Single Line Comments in PHP
echo "Comments Example";
?>
For Multi-line Comments, you need to use /* ... */. Consider following example,
<?php
/*
This is Multiline Comment Example
Where more than one lines are available
in Comments.
*/
echo "Multiline Comment Example";
?>
White Spaces in PHP
White spaces are basically spaces, end line characters , tabs which are not visible on the output screen. In case of PHP, PHP Parser ignores white spaces. Basically these white spaces are used to make code more readable and helps to make it look beautiful. Look at following Example,
<?php
/*
The Following statement
Consisting White Spaces.
*/
$count=5 * 5;
$test = 10;
echo "White Spaces Example". $count;
echo "White Spaces Example". $test;
?>
Blocks in PHP
In PHP, Pair of Curly braces create/makes a block. The block may consist single or multiple PHP statements. For Example,
<?php
/*
This Example Demonstrates
the use of blocks in PHP.
*/
$count=55;
if($count>10)
{
// First Block
echo "Count is Greater than 10";
}
else
{
// Second Block
echo "Count is Less than 10";
}
?>
PHP Expressions and Statements
The Sequence of identifiers creates and expression and Statement is any valid expression which is terminated by semi colon (;). For Example,
<?php
// Example of Statement
$Message="This is Welcome Message";
// Example of Expression
$count=5+5;
echo "Statement is".$Message;
echo "Expression value is".$count;
}
?>
Executing/Running PHP Script in Windows Command Prompt (cmd)
You can also Execute PHP script in Windows Command Prompt(cmd). To Run .php file in Windows Command Prompt (cmd) , Save your file in PHP software Directory. Consider PHP software directory is C:\php. Take following steps :
Step 1: First Create Hello.php file as shown below and save in C:\php.
<?php
// Example of Executing PHP Script in Command Prompt
echo "Welcome to PHP";
?>
|
PHP FIle Hello.php in Sublime text |
Step 2: First now open command Prompt and change directory to c:\php using cd command. Then Type command php Hello.php
|
Command Prompt with location C:\php |
|
Type php Hello.php in command prompt |
Step 3: The output will be Welcome to PHP shown in command prompt.
|
The output Welcome to PHP shown in command prompt
|
I would acknowledge you sharing your individual experiences about "Fundamentals of Php Syntax". If you have any doubt/query related to PHP or Web Technologies, Please let me know in Comment Section.
I will certainly take care that, I should give reply for every single comment on my blog posts. So, let’s fire the discussion up! See you in the comments section below..
Sharing is caring ❤️
Thank you for Visiting PHPBash. Keep Visiting for more Interesting concepts of Php language and web technologies from basics to advanced.
As well we will also share some ready-to-use, useful code snippets for beginners to kick start their web development project.
Do you want to build a modern, lightweight, responsive website and launch quickly?
I help to create the best websites / web applications and will be available for freelance work. contact: phpgems28@gmail.com
Comments
Post a Comment
Thank you so much.