PHP is a powerful scripting language that can be run by itself in the command line of any computer with PHP
installed. However, PHP alone isn’t sufficient for building dynamic web sites. To use PHP on a web site, you need a server that can process PHP scripts. Apache is a free web server that, once installed on a computer, allows developers
to test PHP scripts locally; this makes it an invaluable piece of your local development environment.
Additionally, web sites developed with PHP often rely on information stored in a database, so it can be modified quickly and easily. This is a significant difference between a PHP site and an HTML site. This is where a relational database management system such as MySQL comes into play. This book’s examples rely on MySQL. I chose this database because PHP provides native support for it, and because MySQL is a free, open source project.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
By default, PHP documents end with the extension .php. When a web server encountersthis extension in a requested file, it automatically passes it to the PHP processor. Ofcourse, web servers are highly configurable, and some web developers choose to forcefiles ending with .htm or .html to also get parsed by the PHP processor, usually becausethey want to hide the fact that they are using PHP.
Your PHP program is responsible for passing back a clean file suitable for display in a web browser. At its very simplest, a PHP document will output only HTML. To prove this, you can take any normal HTML document such as an index.html file and save it as index.php, and it will display identically to the original.
To trigger the PHP commands, you need to learn a new tag. The first part is:
<?php
The first thing you may notice is that the tag has not been closed. This is because entire
sections of PHP can be placed inside this tag, and they finish only when the closing part
is encountered, which looks like this:
?>
A small PHP “Hello World” program might look like Example .
Example :Calling PHP
<?php
echo "Hello world";
?>
The way you use this tag is quite flexible. Some programmers open the tag at the start
of a document and close it right at the end, outputting any HTML directly from PHP
commands.
What Is a Variable?
A variable is a keyword or phrase that acts as an identifier for a value stored in a system’s memory. This is useful,because it allows us to write programs that will perform a set of actions on a variable value, which means you can change the output of the program simply by changing the variable, rather than changing the program itself.
Storing Values in a Variable It is quite straightforward to store a value in a variable. In one single line, you can declare a new variable and assign a value to it:
<?php
$myName = "Ayan";
$friendsName = "Ali";
echo "<p>I am $myName and I have a friend called $friendsName.</p>";
If you type the preceding lines into your test.php file and load it in your browser, you should see an output such as the following:
I am Ayan and I have a friend called Ali.
Perhaps you will notice that the preceding code holds nothing but PHP. Consequently, there is no need to mark the end of the PHP block with a PHP delimiter. You can add ?> at the end, if you like; it’ll make no difference.