Introduction To PHP: What You Can Do With PHP
Introduction To PHP: What You Can Do With PHP
Introduction to PHP
PHP is the most popular server-side scripting language for creating dynamic web pages.
PHP stands for Hypertext Preprocessor.
PHP is a very popular and widely-used open source server-side scripting language to
write dynamically generated web pages.
PHP was originally created by Rasmus Lerdorf in 1994.
It was initially known as Personal Home Page.
Portability: PHP runs on various platforms such as Microsoft Windows, Linux, Mac
OS, etc. and it is compatible with almost all servers used today such Apache, IIS, etc.
Fast Performance: Scripts written in PHP usually execute or runs faster than those
written in other scripting languages like ASP, Ruby, Python, Java, etc.
Vast Community: Since PHP is supported by the worldwide community, finding
help or documentation related to PHP online is extremely easy.
You can either install them individually or choose a pre-configured package for your
operating system like Linux and Windows. Popular pre-configured package are XAMPP and
WampServer.
<?php
?>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP Comments
A comment is simply text that is ignored by the PHP engine. The purpose of comments is to
make the code more readable. It may help other developer (or you in the future when you edit
the source code) to understand what you were trying to do with the PHP.
<?php
?>
followed by an asterisk (/*) and end the comment with an asterisk followed by
a slash (*/), like this:
Lecture 17 Web Systems and Technologies
<?php
/* This is a multiple line comment block that spans across more than one
line */
?>
As a result the variables $color, $Color and $COLOR are treated as three different
variables.
<?php
// Assign value to variable
$color = "blue";
// Try to print variable value
echo "The color of the sky is " . $color . "<br>";
echo "The color of the sky is " . $Color . "<br>";
echo "The color of the sky is " . $COLOR . "<br>";
?>
NOTE: However, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-
defined functions are case-insensitive.
PHP Variables
Variables are used to store data, like string of text, numbers, etc. Variable values can change
over the course of a script. Here're some important things to know about variables:
In PHP, a variable does not need to be declared before adding a value to it. PHP
automatically converts the variable to the correct data type, depending on its value.
After declaring a variable it can be reused throughout the code.
The assignment operator (=) used to assign value to a variable.
<?php
// Declaring variables
$number = 10;
?>
All variables in PHP start with a $ sign, followed by the name of the variable.
A variable name must start with a letter or the underscore character _.
A variable name cannot start with a number.
A variable name in PHP can only contain alpha-numeric characters and underscores (A-
z, 0-9, and _).
A variable name cannot contain spaces.
NOTE: Variable names in PHP are case sensitive, it means $x and $X are two different
variables. So be careful while defining variable names.
Since echo is a language construct not actually a function (like if statement), you can use it
without parentheses e.g. echo or echo(). However, if you want to pass more than one
parameter to echo, the parameters must not be enclosed within parentheses.
Display Variables
The following example will show you how to display variable using the echo statement:
<?php
// Defining variables
$txt = "Hello World!";
$num = 123456789;
$colors = array("Red", "Green", "Blue");
// Displaying variables
echo $txt;
echo "<br>";
echo $num;
echo "<br>";
echo $colors[0];
?>