Basic PHP Syntax | PHP Tutorial

PHP Syntax

You can use PHP anywhere in Document because PHP of Syntax is very easy and simple.

See PHP is executed on the server and sent back to us in the form of plain HTML format through the browser. So let's try to understand the basics of PHP syntax.

A PHP script can be placed anywhere in the document. Remember that the file extension of the document should be .php

A PHP script starts with <?php and ends with ?> you can also use inline PHP script like <? ....... ?>

Syntax

<?php PHP Code goes here ?>

Let's Take an Example to print/echo Hellow word.

Suppose any text is to be displayed in PHP, then for that, we write the following code in PHP:

<?php  echo "Hellow Word"; ?>

OR

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP Code To display Text</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

You can also use PHP in HTML attributes and properties like:

<!DOCTYPE html>
<html>
<body>
	<!--Define PHP Variable to use assigne color-->
<?php
$color = 'orange'; // Assigne Value
$link = '#' // Assigne Value
?>
<p style="color: <?php echo $color; ?>;">Html Properties....</p>
<a href="<?php echo $link ?>">HTML Attributes</a>

</body>
</html>

Result:

Html Properties...

Is PHP a case-sensitive language?

See, in PHP, all the variables we make of any name keep different values, so the name of the variable in PHP is case-sensitive.

<!DOCTYPE html>
<html>
<body>
	<!--Define PHP Variable to use assigne Value-->
<?php
$text = 'PHP'; // Assigne Value
$TexT = 'HTML'; // Assigne Value
$TeXt = 'Java'; // Assigne Value
?>
<p>I love <?php echo "$text"; ?></p>
</body>
</html>

Result:

I Love PHP

Apart from this, all the keywords, predefined functions, user-defined functions, and classes in PHP are not case-sensitive.

Note - The PHP statements are terminated by a semicolon if it is uncompleted then shows an error.

Post a Comment

0 Comments

Comments