Search

Saturday, July 7, 2012

How to Write PHP Scripts


PHP is a server scripting language used to make web pages interactive. Think of what happens when you edit a page on this website, for example. When you submit your changes, they're sent to one of the wikiHow servers, where they're processed by PHP scripts, and a new version of the page is sent back to your browser. Behind this process are many, perhaps hundreds of PHP scripts, controlling how web pages change based on a variety of circumstances.


While creating the software that powers this wiki is a monumental task, the programming itself is actually very approachable, even if you've never had any training as a programmer. This article will teach you how to write a few very simple PHP scripts so that you can get a basic understanding of how PHP works.


Install the Apache Web Server.


Install the PHP Engine on Your Computer.

Echo
Open Notepad (File>>All Programs>>Accessories>>Notepad). You'll be using Notepad a lot.

Type this into Notepad:

<?php echo "Hello World!"; ?>


Save this file as "helloworld.php".



When saving files in Windows Notepad, wrap the filename in double quotations so that Notepad doesn't add .txt to the end of the filename. Notice in the image that the file helloworld.php has " " around it. You have to do this or Notepad will save the file as helloworld.php.txt


Or you can simply click the drop down for Save as type and change it to All Files (*.*) which will leave the name exactly how you type it and the quotes will not need to be added.







Open your favorite browser and type this address in the address bar:http://localhost/helloworld.php

  • If you receive an error message, make sure you typed in the code exactly as shown above, including the colon.

  • This is what you should see in your browser window:

Hello World!

Now it's time to understand how it works.

<?php ?>

These two lines tell the PHP engine that everything between them is PHP code. Everything outside the two tags is treated as HTML and ignored by the PHP engine and sent to your browser the same as any other HTML. The important thing you need to recognize here is that PHP scripts are embedded inside regular HTML pages.
echo "Hello World!";

This line uses the echo statement (well, actually it is a construct, but more on that later). Statements are used to tell the PHP engine to do something. In this case, you are telling the engine to print what is inside the quotes.
It's important to know that when we say print, we don't actually mean print. The PHP engine never actually prints anything to your screen. Any output generated by the engine is sent to your browser as HTML. Your browser doesn't even know that it's getting PHP output. As far as the browser is concerned, it's getting plain HTML.




PHP and HTML
Change the script above so that it looks like this:

<?php echo "<strong>Hello World!</strong>"; ?>


Save the file as "helloworld2.php", and open your browser by using the address:http://localhost/helloworld2.php

This time the output is the same as before, but this time the text is in bold.
<strong> and </strong> are HTML markup tags the tell the browser to show any text between them as bold. As stated earlier, a browser will treat anything that prints out of a PHP engine like HTML.

Let's write the same script as before, but add another echo statement. 

Type this into Notepad:
<?php echo "Hello World!<br />"; echo "How are you doing?"; ?>

Save this as "helloworlddouble.php"
When you run the script in your browser, you should see this:
Hello World!How are you doing?

The thing you need to notice is the <br /> on the first line. This is HTML markup to insert a line break. If you didn't add this, your output would look like this:

Hello World!How are you doing?

Now that you've mastered the echo statement, let's move on to something a bit more complicated.

Variables
Think of variables as containers for data. To manipulate data, be it numbers or names, you need to store the data in a container. The syntax for assigning data, or better called a value, into a variable is:


$myVariable = "Hello World!";

In the above example, the value is "Hello World!", and the variable is $myVariable. You're telling PHP to store the value at the right of the equal sign, into the variable at the left of the equal sign.

The dollar sign ($) at the beginning tells PHP that $myValue is a variable. All variables must start with the dollar sign.



Now let's do something fun with a variable. Open Notepad and type this in:


<?php $myVariable = "Hello World!"; echo $myVariable; ?>

Save the file as "myfirstvariable.php". When you browse to this script with your browser, the output should look like this:

Hello World!

Let's examine in how it works. On the first line of the script, you have defined a variable called $myVariable, and you have inserted a value; "Hello World!". On a line like this, you are giving the variable name on the left of the equal sign, and the value you want to assign to it on the right of the equal sign. From this point on in your script, unless you change the value, $myVariable will always contain the value "Hello World!".

The next line you should be familiar with, a simple echo statement. What you need to take notice of is that the echo statement printed the value of $myVariable, and not literally "$myVariable".

Variables can also contain numbers, and then those numbers can be manipulated using simple mathematical functions.
Take this next script, for example. Type this into notepad:


<?php $mySmallNumber = 12; $myLargeNumber = 356; $myTotal = $mySmallNumber + $myLargeNumber; echo $myTotal; ?>

Run this script in your browser, you should see this:

368

Nothing real impressive, but you did add two variables together. Let's review.

On the first two lines, you created two variables. $mySmallNumber, and $myLargeNumber. You have inserted into these variables two values, 12 and 356, respectively.

On the third line, you have created another variable called $myTotal. With this variable, you have stored the value of $mySmallNumber PLUS $myLargeNumber. Since $mySmallNumber has the value of 12 stored in it, and $myLargeNumber has the value of 356 stored in it, what you have done is stored the value of 12 + 356 into the $myTotal variable. The value stored in $myTotal is 368.

The last line is an echo statement which outputs the value of $myTotal.




tips

This article assumes you've installed Apache and PHP on your computer. Anytime it's said to save a file, you are saving in the\htdocs directory inside the Apache directory.
A really useful tool to help you test PHP files is XAMPP which is a free program that installs and runs Apache and PHP to help you simulate a server on your computer.

No comments:

Post a Comment