PHP was developed to be a preprocessor language but it lost it track for good and now it has become the de facto language for dynamic websites.
PHP has given tough time to its rival ASP and now it is on top.
PHP is Object Oriented like C++. Its syntax are something similar to C/Perl/C++ and if you are coming from C/C++/Perl background then you will find it easier to learn.
PHP is server side scripting language and requires a database to show its magic. However it doesn’t stop you using it without database.
I prefer PHP over HTML and you may observe that most of my websites are in PHP (without database)
You must learn at least mySQL (Preferred database for PHP) to learn the full potential of PHP/mySQL combination.
And most importantly PHP is free.
PHP is portable meaning you can write the script and execute it on any web server including Apache, IIS etc.
PHP is the strangest language I’ve ever known. None of other languages use same syntax as PHP.
A typical PHP code starts with <?php and ends with ?>
And you need to be careful with the spaces. Any space between < and ? means you see nothing in web browser.
Here is our first PHP script
<?php echo “Namaskar”; ?>
Humm!!! where is the typical “Hello World!!” ? Well its my style so live with it. Namaskar is greeting in Hindi and it is used for evrything from Hi/Hello/Good morning/Afternoon/Night to Bye.
And yes did you recognize ; didn’t I say PHP is similar to C/C++.
And the following would be our full code embedded inside html
Copy & paste above code and save in a file with .php (e.g. file.php ) extension and put it on your server.
Access this file from your browser (http://www.yourdomain.com/file.php or http://localhost/file.php if you are running on WAMP on your own local server) and you will see your first successful php script.
PHP comments are borrowed from C/C++ and support both single line C++ style comments and C style block comments
// This is a comment in PHP
This is a comment
As with every other programming languages PHP rely on variables. Variables are those entities which hold data.
In PHP a variable name starts with $ sign.
$thisIsAString = “Namaskar”;
$myLuckyNumber = 12345;
Humm!!! excuse me what about the variable type? How does PHP know which variable is going to store what?
Well PHP is loosely typed script meaning that it doesn’t need to know the type of variable in advance. This is really cool isn’t it? you don’t need to worry about C++ type_cast styles any more.
Simple rules for variable names
1) A variable name must start with a letter or underscore _
2) A variable name must contain only alphanumeric characters and underscores.
E.g. a-z, 0-9, and _
Wow! very simple rule can, be remembered easily.
You can’t think of any language without strings can you?
Think of perl and you know what I mean.
As explained in my previous tutorial a string can be declared as variable. A string can also be used directly instead of assigning it to a variable and using it.
Lets take an example.
$string1 = “Namaskar”;
$string3 = $string1 . ” ” . $string2 . “!”;
The echo will print
We have seen some of string operations in the above code also.
. is a concatenate operator used to concatenate two or more strings.
You can find rest of string related functions on PHP language home page http://www.php.net
Include and require functions
Include and require functions are used to include files.
<?php include(“leftMenu.php”); ?>
<?php require(“leftMenu.php”); ?>
Difference between include and require
With include the rest of code will execute even included files were not found whereas require will not execute any code if it finds any error.