Syntax of PHP

Posted on Updated on

ijioj8578Basic PHP Syntax

A PHP language can be located anywhere in the html page.

A PHP script starts with <?php and ends with ?>:

<?php

// here we can write the php code

?>

The default file extension for PHP files is “.php”.

A PHP file normally contains HTML tags, and a number of PHP encode.

Lower, we have an case of a clear PHP file, with a PHP language that use a built-in PHP task “echo” to output the text “hi buddy!” on a web page:

Example

<!DOCTYPE html>

<html>

<body>

<h1>my first php page </h1>

<?php

echo “hi buddy!”;

?>

</body>

</html>

Note: PHP statements end with a semicolon (;).

Comments in PHP

A comment in PHP encode is a line that is not executed as part of the program. Its only reason is to be study by somebody who is looking at the code.

Comments can be used to:

 Let others know what you are doing

 Remind yourself of what you did – Most programmers have skilled coming back to their own job a year or two later and having to re-figure out what they did.

Comments can hark back you of what were your idea when you wrote the code PHP provide some ways of commenting:

Example

<!DOCTYPE html>

<html>

<body>

<?php

// here I show you single-line comment

# it is also a single-line comment

/*

here I show you multiple-lines comment block

that spans over multiple

lines

*/

// here You can also use comments to leave out parts of a code line

$x = 5 /* + 15 */ + 5;

echo $x;

?>

</body>

</html>

PHP Case Sensitivity

In PHP Language,keywords (like if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.

In this example we can see all three echo statements below are legal (and equal):

Example

<!DOCTYPE html>

<html>

<body>

<?php

ECHO “Hi buddy!<br>”;

echo “Hi buddy!<br>”;

EcHo “Hi buddy!<br>”;

?>

</body>

</html>

but all variable names case-sensitive.

In the example below, only the first statement will display the value of the $res variable (this is because $res, $RES, and $reS are treated as three different variables):

Example

<!doctype html >

<html >

<body >

<?php

$res = “red”;

echo “My pen is ” . $res . “<br>”;

echo “My house is ” . $RES . “<br>”;

echo “My keyboard is ” . $reS . “<br>”;

?>

</body>

</html>

PHP Constants

A constant is an identifier (name) for a easy value. The value can’t be tainted during the script.

A valid constant name starts with a letter or underscore (we are not use $ sign before the constant name).

Disparate variables, constants are automatically global across the whole script.

Build PHP Constant

To build a constant, use the define() function.

Syntax

define( name,  value,  case-insensitive)

Parameters:

name: it tells the name of the constant

value: Specifies the value of the constant

case-insensitive: whether the constant name should be case-insensitive. Default is false

The example below creates a constant with a case-sensitive name:

Example

<!DOCTYPE html >

<html >

<body >

<?php

// here we declare case-sensitive name of the constant

define(“wish”, “hello dear how uhh!”);

echo wish;

?>

</body >

</html >

Out put

Hello dear how uhh!

The example below creates a constant with a case-insensitive name:

Example

<!DOCTYPE html >

<html >

<body >

<?php

// here we declare case-insensitive name of the constant

define(“wish”, “hey dear what are you doing!”, true);

echo wish;

?>

</body >

</html >

Output

Hey dear what are you doing!

Constants are Global

Constants are automatically global and can be worn crossways the entire program.

This example below uses a constant inside a function, even if it is defined outside the function:

<!DOCTYPE html >

<html >

<body >

<?php

define(“wish”, “Welcome dear!”);

function funs_wel() {

     echo wish;

}

funs_wel();

?>

</body>

</html>

Output

Welcome dear!

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s