PHP Syntax

PHP (Hypertext Preprocessor) is a popular server-side scripting language primarily used for web development. When writing PHP code, it’s essential to understand the basic syntax that dictates how PHP scripts are structured.


Basic PHP Syntax


  • A PHP script can be placed anywhere in the document.


  • The opening tag <?php tells the server to interpret the enclosed code as PHP.


  • The closing tag ?> ends the PHP code. This closing tag can be omitted if the file is pure PHP code to avoid unwanted output, like whitespace.


  • These tags are also called ‘Canonical PHP tags’. Everything outside of a pair of opening and closing tags is ignored by the PHP parser.



<?php
// PHP code goes here
?>

Note

Every PHP command ends with a semi-colon (;).

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

Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>

Try it yourself


PHP Case Sensitivity


PHP keywords (e.g., if, else, while), functions, and classes are not case-sensitive, but variable names are case-sensitive.



Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>

Try it yourself

Note

However; all variable names are case-sensitive!

Look at the example below; only the first statement will display the value of the $color variable! This is because $color$COLOR, and $coLOR are treated as three different variables:

Example
<!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>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.