PHP 7 Syntax
Basic PHP 7 Syntax
Read previous part here. before proceeding to PHP 7 Syntax
A php script is executed on the server, and plain HTML result is sent back to the browser.
A script can be placed anywhere in the document.
A script starts with
<?php and ends with
<?php // PHP code goes here ?>
The default file extension for PHP files is “.php“.
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example(for Basic PHP 7 Syntax) of a simple PHP file, with a PHP script that uses a built-in PHP function “
echo” to output the text “Hello World!” on a web page:
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
Note: PHP statements end with a semicolon (
Comments in PHP
A comment in code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code.
Comments can be used to:
- Let others understand what you are doing
- Remind yourself of what you did – Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
PHP supports several ways of commenting:
<!DOCTYPE html> <html> <body> <?php // This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block */ // You can also use comments to leave out parts of a code $a = 35 /* + 105 */ + 65; echo $a; ?> </body> </html>
PHP Case Sensitivity
In PHP, keywords (e.g.
echo, etc.), classes, functions, and user-defined functions are Not case-sensitive.
In below example , all three echo statements below are equal:
<!DOCTYPE html> <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>
However; all variable names are case-sensitive.
In below example, only the first will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different variables):
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?> </body> </html>
My car is red My house is My boat is
Also visit:- techdevil.in