Wednesday, August 15, 2007

PHP Variables

Variables are containers which hold values. This is useful when you need to use the same piece of information several times, or on several different pages, because it enables you to easily pass information between functions and documents, which is a big part of having a dynamic web page.




A variable is a name that you choose beginning with a dollar ($) sign. You set the variable with the assignment operator, otherwise known as the equals (=) sign.




$name = "Kali";

$age = 21;





You can then call on the variable later in the script. Here is an example.

<?php
$name = "Kali";
$age = 21;

print("Hi there, my name is $name. I am $age years old.");

?>


The script above will then print out: Hi there, my name is Kali. I am 21 years old.




When naming variables, make sure that it uses only letters, numbers and the underscore (_) character, and no spaces. It also cannot begin with a number, so $4bottles is not valid, but $four_bottles is. Remember that variable names are case sensitive.





So how would you use variables? A good example is with a HTML form that gathers user input, then uses that input on another page, like a guestbook does.

PHP Useful Tips

You can combine PHP with HTML, like so:

<html>
<body>

<font face="Arial,Helvetica">

<?php
print "my first PHP script!";
?>

</font>

</body>
</html>


As long as you still name the file with a .php extension, the server will check the code for tags, interpret them, and send them along to the browser along with the HTML code, so that when it has loaded it just looks like a regular HTML page.








You can add comments to your PHP code like so:

<?php
// print some text
print "my first PHP script!";
?>


Everything on a line beginning with two slashes (//) will be ignored by the server. Comments serve as reminders, and are useful when you go back to the code months later to edit it. If you have comments that span over more lines than one, use the following comment tags:

<?php
/*
print some text
this is a comment
over more than one line
*/
print "my first PHP script!";
?>

Monday, August 13, 2007

PHP Introduction

PHP is a server-side scripting language usually written alongside HTML. The difference between them is where HTML is interpreted directly by the user's browser, a PHP script is first interpreted by the server, executed, and then sent to the browser along with the HTML. Thus, the server your PHP script is on needs to be able to support PHP; you'll have to ask your host.




PHP code is very different from HTML code, however if you already know HTML then PHP shouldn't be hard to learn. Just like HTML, PHP has tags:






















Start TagEnd Tag
<?php?>
<script language="php"></script>
<??>
<%%>






Above are four different styles of tags, but they all mean the same thing. They're telling the server that in between the tags is some PHP code that needs to be interpreted. The top two sets of tags will always work on a server that has PHP enabled, however the last two may not, as they must be enabled seperately by the server administrator.




Now, open up NotePad, or another plain text editor, and let's try our first PHP script. Oh, don't look so worried; it's too easy:




<?php
print("my first PHP script!");
?>


Here, the print() function will send the data (a collection of characters called a string) between the parenthesis to the browser. Strings must always be surrounded by either single or double quotation marks, however if the data is a number or a boolean (true/false) value, then it doesn't need quotation marks. The semicolon (;) ends the PHP statement.




You can also have all the code on one line, if you prefer, like this:

<?php print("my first PHP script!"); ?>


That's it! Save the file as "test.php" and upload it to your server. It's important that you save it with a .php extension, otherwise the server won't know to interpret the script.




When you call the file in your browser, it should display a line of text that says my first PHP script! If it doesn't, make sure you've named the file with a .php extension, and that your server supports PHP.