Monday, February 7, 2011

Step#3 Variables in PHP

Variables are used for storing values, like text strings, numbers or arrays.


When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.

The correct way of declaring a variable in PHP:

$var_name = value;    

New PHP programmers often forget the $ sign at the beginning of the variable. In that case it will not work.
Let's try creating a variable containing a string, and a variable containing a number:

<?php
$txt="Hello World!";
$x=16;
?>

Wednesday, February 2, 2011

Step#2 Basics Syntax of PHP

PHP is a very simple language. If you are familiar with C++ or Java then it will be a plus point for you, but If you dont know then dont worry its not a hard thing to learn.

A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.
On servers with shorthand support enabled you can start a scripting block with <? and end with ?>.
For maximum compatibility, I recommend that you use the standard form (<?php) rather than the shorthand form.

<?php
?> 


A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
Below, I have an example of a simple PHP script which sends the text "Hello World" to the browser:

Open Notepad and  create a new file, here you will write your all PHP code.

<html>
<body>

<?php
echo "Hello World";
?>

</body>
</html> 

Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print. In the example above I have used the echo statement to output the text "Hello World".

Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be executed.

Execute your first PHP program

Now do some action, for executing your just written first PHP script, first save it in your WAMP directory and fallow the following path
"C:\wamp\www" in www folder create a new folder name it "php" or something else you like and save your PHP file here. Then run your PHP file in web browser (I will recommend Mozilla Firefox) by typing its address, like "http://www.localhost/php/your file name .php". You can see the output.

Tip: If you name your file "index.php" then server will take this file as default file and open it first among many files. For example if you name your file index.php then no need to type file name in the address bar only http://www.localhost/php/ will also work.

Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.

<html>
<body>

<?php
//This is a comment

/*
This is
a comment
block
*/
?>

</body>
</html>

Step#1 Prepare your Computer for PHP


At First step you will need to prepare your PC for PHP, don't worry I will teach you how.

PHP is a server side language that runs on a server PC, but you can run it on your PC at the same time as Server and Client. I will recommend you WAMP server for this.

What is Wamp Server? Well I will not go in the details (our main focus is PHP) but I will tell you a little about it because you will use it to run PHP on your PC.
"WAMP is a form of mini-server that can run on almost any Windows Operating System. WAMP includes Apache , PHP  , and MySQL."  Basically WAMP comes with a package of following three  softwares.

1:  Apache  - A Web Server that serves the web requests.
2:  PHP  - PHP language package.
3:  MySQL - Database Server.

Setting up Wamp Server locally in Windows XP

The great thing about WampServer is it makes our job easier by installing Apache server, MySQL database and PHP all at the same time. Don’t worry about installing or how these will interact with each other.

  • Download the latest version of Wamp Server- as of this writing it is of Version 2.0i. (Approximately 16 MB)
  • Run the setup process by double clicking the downloaded WampServer2.0i.exe file.
  • Click on “Next” and check the radio selection “I accept the agreement” and press “Next”.
  • The default path for WampServer is “C:\wamp“. You can change it if you want by clicking the “Browse” button but I do suggest you leave this in the default setting and press “Next“.
  • Select both “Create a Quick Launch Icon” and “Create a Desktop Icon” and press “Next“.
  • Click “Install” to begin with the installation.
  • A popup box will appear and ask you to choose your default browser. Please choose your default browser to which the wamp server will be associated. I do suggest that you use Mozilla Firefox as there are so many plugins that you can add to this browser to make your programming world become more interesting and efficient. Click “Yes” to continue.
  • Unblock” the Windows Security Alert if it asks you.
  • Just leave the default values as such for the “PHP Mail Parameters” and click “Next“.
  • Click “Finish” to exit the setup (Make sure that the “Launch WampServer 2 now” option is checked)
  • Now you will see a “speedometer- like icon” on the notification area of the taskbar (beside your taskbar clock).
  • Just wait until the speedometer is fully white (Represents its fully ready).
  • If your WampServer is fully ready, you can skip the next step and continue reading “Setting Up phpMyAdmin“.
  • If it is not, that means some other server service is running in the background and the service with WampServer is clashing.  Most probably you have windows web server (Internet Information Services – IIS) running at the time of the WampServer installation. To solve this, we need to stop the IIS. Same things goes if you want to start IIS services, you must stop the WampServer services.
  • To stop the IIS service, go to Start > Run > and type services.msc and hit the enter button. This will opens up a services control panel.
  • On your right hand side, navigate and find a service called “IIS Admin“. Right click on it and select “Stop“. This will stop the IIS services.
  • Now you can restart your WampServer services by single clicking on the WampServer icon, and select “Restart All Services“.
  • You can now see your speedometer-like icon is a full white color (Represents its fully ready).
  • Your Wampserver is now ready. Now open web browser and type http://localhost in the address bar this will open a welcome page of WAMP server.

Tuesday, February 1, 2011

About me and the blog


Hello!

My name is Mamoon Rashid.  I have done my BS in Computer Sconces from Allama Iqbal Open University Pakistan in 2006 and nowadays I am working in City Traffic Police Rawalpindi as Incharge IT. I am also a PHP freelancer working on oDesk as part time PHP Web Developer.

The Purpose of  developing this blog is to express my core knowledge about PHP to the new comers. I will try to teach you the PHP, Step BY Step with the help of useful examples

At the end I will also teach you that how you can make money online with the help of your PHP knowledge, as I am doing :)

Important: Before you start reading this blog make it sure that you know the basics of HTML.