The PHP script or code executes on the server and returns the results to the normal HTML format in the browser.
PHP basic structure (syntax)
The PHP script can be placed anywhere in the document on the server.
PHP default file extension is “.php”.
The default delimiter syntax opens with <?php
and concludes with ?>
, like this:
Code Example:
<?php
// Some code will be run
echo "Hello, CodeGrope!";
?>
This will produce the following result:
Hello, CodeGrope!
Each PHP statement ends with a semicolon (;
) – it tells the PHP engine that the current condition is finished.
Short-open Tags
Php script components are included in the exclusion syntax. There are four separate landing formats, in which the little known short open tag, which looks like this:
<? echo “Hello, CodeGrope!”; ?>
This will produce the following result:
Hello, CodeGrope!
You can share this syntax with XML, which can cause problems in some environments. So, a way to disable this specific layout has been provided.
Embedding PHP Code in Your Web HTML Pages
PHP file is simple text files with .php extension. You can write HTML within the PHP file as you work on regular HTML pages. One advantage of PHP is that you can embed PHP code as well as direct code. For the code to try and do something, the page should be passed to the PHP engine for interpretation.
Code Example:
<!DOCTYPE html>
<html>
<head>
<title>Embedding PHP Code within HTML</title>
</head>
<body>
<h1><?php echo "Hello, Code Grope!"; ?></h1>
</body>
</html>
This will produce the following result:
Hello, Code Grope!
The above example shows how you can embed PHP codes for HTML-based dynamic web pages. If you know the difference between web page source code as a browser, then you can see that PHP code <?php
echo “Hello, CodeGrope!”; ?>
See output Hello, CodeGrope!
When you run this code, php engine <?php
….. ;?>
highlights the guidelines for the tag and the web server sends the output to the full HTML in your browser.
PHP Code Comments
A comment is only text that is disregarded by the PHP engine. PHP comment line that is not read/executed as part of the program. The purpose of the comment is to help other developers (in the future, when you edit the source code) to understand what you are trying to do with PHP. There are two commenting formats in PHP:-
- Single-line comments
- Multi-lines comments
Single-line comments: This is used for short explanations or notes. To write a single-line comment, start the line with two slashes (//
) or a hash symbol (#
). Here are examples of single line comments:
<?php
# This is a comment
// It’s a comment too. Each style only comments
echo “An example with a line comment”;
?>
This will produce the following result:
An example with a line comment
Multi-lines comments: PHP multi-line comments are used to provide the detailed explanation. To write multi-line comments, start (/*
) and end the comment (*/
). Here is the example of multi-line comments.
<?php
/* This is multi line comment
Author : Code Grope
Subject: PHP
*/
echo “An example with multi line comments”;
?>
This will produce the following result:
An example with multi line comments
Whitespace Insensitive in PHP
PHP whitespace is obsolete, that does not matter how much white pattern you have in any row. one whitespace character is that the same as several such characters. Here are examples of whitespace insensitive:
Code Example:
<?php
$six = 3 + 3; // single spaces
$six =3+3 ; // spaces and tabs
$six =
3+
3; // multiple lines
?>
PHP is case sensitive
All PHP keywords such as statement, classes, functions, user-defined functions are not case-sensitive. The following three echo statements are lawful and same. Try out following example –
Code Example:
<?php
ECHO "Hello Codegrope! ";
echo "Hello Codegrope! ";
EcHo "Hello Codegrope! ";
?>
This will produce the following result:
Hello Codegrope!
Hello Codegrope!
Hello Codegrope!
If you are trying to run the example code above it will display all the output.
PHP all variable name is case-sensitive. As a result, the variables $name, $Name and $NAME are behaving as three different variables. Try out following example –
Code Example:
<?php
// Assign value to variable
$name = "CodeGrope";
// Try to print variable value
echo "My Website name is " . $name . " ";
echo "My Website name is " . $Name . " ";
echo "My Website name is " . $NAME . " ";
?>
This will produce the following result:
My Website name is CodeGrope
If you are trying to run the above example code it’ll only show the value of the variable $name and turn out the “Undefined variable” warning for the variable $Name and $NAME.