Basic PHP Intro

I’ll assume you’ve already got an acceptable place to try out PHP
scripts. Usually, you’ll do this on a web server. You can also install
a web server onto your own computer and PHP-enable it. Though, it is
possible to write command-line scripts with php as well. We’ll delve
into that in a later lesson. For now, we’ll focus on using PHP to
generate basic HTML pages and I’ll assume that you already know how to
put pages up for download and whatnot. Knowing HTML is fairly important if you wish to make it very far with web-based PHP.

Section 1: Printing text to a page

Let’s start out with the most basic of all scripts: Write this in
notepad or a similar text edittor and save it as “helloint.php” and
upload it to your web server.

<?php

//// Say on the page Hello Internet!

print "<b>Hello Internet!</b>";

?>

After you have done this, point your browser to it by saying: http://example.com/helloint.php

It should show Hello Internet when you visit the web page.

Here’s how it pretty much works: PHP sees the “<?php” and starts
interpretting the stuff following it as PHP scripting. ?> 
tells it to switch back to HTML-mode.

Two
slashes will let you leave a comment for yourself or other coders
looking at your PHP script. Everything after two slashes on a line will
be ignored by PHP allowing you to leave human-readable notes behind.

That moves us along to the print statement. You can use this statement
to send text enclosed in double-quation marks, or single quotation
marks (among other things as you’ll see later) to the person viewing
the PHP page on the web server. Usually the person viewing the page is
looking at HTML rather than some other type of data. However, other
kinds of data can also be spewed out by php. In the meantime, we’ll
just deal with text and HTML though okay? 🙂

Also take notice, statements almost always end with a semicolon. “;”

Intro to PHP Variables and concatenation

For those new to programming: variables are what we will use to store
pieces of information in. If this is a confusing concept, it’ll
hopefully become clear soon.

In PHP, variables can have almost any name. The only rule is that it
cannot start with a number or use any special symbols. Also, when you
set or use a PHP variable, it’ll usually have a US dollar symbol before
the name. Also note that the names are CASE-SENSITIVE. $home is
different from $Home

Names: 
<?php
$var_name = "Colette";
$var_NAME = "Presea";

//Send these variables out to the browser
echo "$var_name, $var_NAME";
?>

The above code will output the following (except not bolded): Names: Colette, Presea

Another piece of code which will do the exact same thing is:

Names: 
<?php
$var_name = "Colette";
$var_NAME = "Presea";

//Send these variables out to the browser
print $var_name . ", ". $var_NAME;
?>

When not used inside of quotes, the period is a concatenation
symbol. You can use it to connect strings together to form sentences
and other things. For instance:

<?php
echo "Welcome "."to "."the "."world "."revolution.";
?>

This will make it print the text “Welcome to the world
revolution.” There are other ways to display text like this which we’ll
cover in a later lesson.

Doing Arithmetic

As with any other scripting language, you can also use PHP to do math.
(In fact, you’ll probably do that a lot.) Doing math in PHP is
relatively easy thankfully.

<?php
$num1 = 23.60;  //  I forgot to mention this earlier: a lone equal sign
$num2 = 1.086;  //  is an assignment operator.  That is to say
                //  the equal sign SETS a variable.

$result = $num1 * $num2;

print "A $num1 purchase costs $result with ". 
($num2 - 1)*100 .
"% tax.<br>";  //You can make long lines more readable
                    //By making them use more than one line.                    

$result = $num1 / $num2;

print "$num1 divided by $num2 = $result<br>";

$result = $num1 + $num2;

print "$num1 + $num2 = $result<br>";

print 5+8*2 . " == 5+8*2"  .".";
?>

These are some quick and dirty examples of doing math and showing it’s
output in PHP. Also, it follows proper order of operations. That is
5+8*2 will evaluate to 21, not 26.

Output:
———–
A 23.6 purchase costs 25.6296 with 8.6% tax.
23.6 divided by 1.086 = 21.7311233886
23.6 + 1.086 = 24.686
21 == 5+8*2.

Thanks for taking the time to read this, that’s all for this lesson, some back soon, and I may have new lessons available.

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>