Posted: Thu Aug 16, 2007 6:00 pm Post subject: PHP 101
Ok, I searched the forums for the php basics and din't find anything. So, I'm going to post some basic php.
To start programming in PHP, you'll need to use the tags:
Code:
<? ?>
OR
Code:
<?php ?>
Now that you've learned the tags, its to to print text into a php page:
Code:
<?
echo "Hello! I know where you live";
?>
OR
Code:
<?
print "Hello! I know where you live";
?>
The ; tells the echo command that its done echoing and it can move onto the next line of code. You can use either print or echo to display text.
Variables:
Variables are very useful in PHP. They can store data to later be retrieved. I'm going to show you hopw to define a variable and then retrieve it.
Code:
<?
$variable1 = "This is a variable";
echo "$variable1";
?>
Again we see the ; being used to tell the script to move on to the next line of code.
Use of the 'rand();' function:
The rand function is basically a piece of code that generates a random number. It can only be used for numbers nothing else. Here is an example using the rand function:
Code:
<?
$number = rand(1,1000);
echo "Your lucky number is: $number!":
?>
Again, we use a variable to store the random number. Also we see rand(1,1000);
This tells us to generate a random number between 1 and 1000.
Anyways I hope this helps and I'll be posting more php related things later on.
Also note there is a difference between the two types of quotes.
"$var" translates into (contents of variable $var), this is called interpolation
'$var' means just that, $var.
To join strings/variables together (alternative to using interpolation, or where it would make things clearer) use the period operator (.), known as the concatenation operator.
Code:
echo 'Your lucky number is:' . $number . '!'
Or
Code:
$var = "moo";
$var .= " said the cow"; //.= is similar to +=, a .=b is equivalent to a = a . b
When using PHP for web development (it is possible to use it for other things) it is important not to feel trapped in PHP or HTML, you can flip between either.
Code:
<a href=<? echo $url ?>><? echo $linkname ?></a>
PHP is a language of convenience - if it will make your life easier (at the cost of elegance) PHP probably lets you do it. For example, rather than doing something like
PHP is quite a fun language, no matter what anyone says!
~nog_lorp _________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum