PDA

View Full Version : PHP: Getting started Part.2


Fenty
06-24-2006, 02:54 PM
Hoping you have read this article we can now continue on to teach you more.

In this part we will have a look over Constants and variables.Now, open your php editor (notepad, word, dreamweaver etc) and make a new file called movie.php

and type the following (if you use dreamweaver it should already have certain tags pre-typed)

<html>
<head>
<title>My Movie Page</title>
</head>
<body>
<?php
define ("BESTMOVIE", "The Fast and The Furious");
echo "The best movie out of all three is ";
echo BESTMOVIE;
?>
</body>
</html>
How it works?
By defining the constant known as BESTMOVIE, you set the value of this to "The Fast and The Furious" in this example where as it could be set to anything you wish. This constant can be recalled and displayed later on throughout the code. Constants arent the best to use in your programs simply because they cant be changed so easily, but its there to be used if you need it. And that my friend is basically a constant.

Define the constant, and then the value it represents.

Now, onto something better than constants....Variables! Now unlike constants variables are obviously ment to be variable (To be changed or ment to change at somepoint in your program (script)). PHP Supports a variaty of different variable types: Strings, arrays, integers, floating point numbers etc

Every variable is denoted with a dollar sign ($) and are case-sensitive so $this is not the same as $This or $THIS. Each variable MUST start with either an underscore or a letter, for example $this and $_this are both valid PHP variable names where as, $121 or $666devil are not valid and will not be parased. <html>
<head>
<title>My Movie Page</title>
</head>
<body>
<?php
//Set variables
$name = 'Tokyo Drift';
$year = '2006';
$number = 'third';

//Print Output
echo 'The best fast and the furious film is the <b>$number</b> released called, <b>$name</b> released in <b>$year</b>';
?>
</body>
</html>
That is an example of variables being used in a simple situation. And as you can see html can also be used inside <?php ?> tags.

How does the example work?
Well the variables $name, $year and $number are firstly defined with the values and then called with the echo function and displayed accordingly to the browser with the output:

The best fast and the furious film is the third released called, Tokyo Drift released in 2006

Well that concludes for this lesson hope you learned something new if not.....then you've done this before!

Copyright Notice:
This article is copyright, The-Wragg 2006. All Rights reserved
Source Code within this article is provided with no warranty or support.
The source code supplied is for illustrative purposes only.
Copyright Infringement is a violation of the law.

random
06-24-2006, 03:05 PM
good tut's, i gotta question, why cant constants be used in the second example wouldn it have the same output?

Fenty
06-24-2006, 03:14 PM
Constants could be used I guess, but in that second example its just basic variables. Variables are better used with databases which I will cover sometime soon. Like i said above, constants stay the same all the time theres no way to change it except manually editing. Where as the variables are changed depending on certain data in a database allowing you to repeat a query but get different results.

kartel
07-06-2006, 03:28 PM
thnxs for part,1 & 2