Basic PHP : The Full Language Introduction 1
Thobela Kgoshi ...
Please note that this tutorial is part of the Build Your Own Site series of tutorials. Feel free to restart the series.
"Alright, alright, alright ..." - Matthew McConaughey ...
Let's get into it Partner.
In the last tutorial of the series; 'Build Your Own Site', we elaborated a lot more on HTML, and helped built our first Form on an HTML Page. We talked a little about submitting forms and needing PHP to process that form submission. And so here we are; learning some of that PHP "good-good" to get you start.
"Alright, alright, alright ..." - Kevin Hart's Dad
Now that we have talked extensively about how all of this is going to work, let's get back to the topic at hand shall we; learning how to use the PHP language.
PHP Tags
As we've indicated in a lot of our previous tutorials, PHP is written between php tags, like so:
<?php exit('soul train'); //php here ?>
<!DOCTYPE HTML>
<html>
<head>
<title>PHP FOOL!!</title>
</head>
<body>
php above fool
</body>
</html>
"basic.php"
, save it and load it in your browser.
Now let's get cracking partner ...
We're gonna add php tags and get coding right away
<?php
echo 'hello world';
?>
"http://localhost/basic.php"
. You'll see the text "hello world", on the otherwise white and empty page.
Okay ... let's get going ...
As with many languages, we're first going to learn how to declare variables in PHP
DECLARING VARIABLES
Variables in PHP are declared with the $
sign, because we're all about that moolla booiiyyy. Naaahh it's just a convention that was adapted at the creation of the language. Look at the example below
<?php
$stuff = "Hello World";
echo stuff;
?>
"$stuff"
, so when we print it on the page using the echo function, we'll get the "Hello World" text as before.
Copy the code above and replace the one in your "basic.php"
file. Reload the page again and voila!
Think of variables as value storage containers, they're called variables because their values are variable, their values can be changed. varied.
Variables values, can be assigned, like we've done above, and a variable's value can be changed along the way within the code. Eg:
<?php
$stuff = "Hello World";
echo $stuff;
$stuff = "New value";
echo $stuff;
?>
"basic.php"
and behold the wonder of updating a variable.
PHP variables, like other languages, can be concatenated with existing text and printed on the page. Look at our updated code below.
<?php
$stuff = "Hello World";
echo "At first you have to say ".$stuff;
?>
$stuff
variable. There are other ways to combine strings, php variables , and html tags in the PHP language. But for now, this is all you need to know. We'll use and look at a couple of those methods throughout these tutorials. So hang tied soldier, you're about to get educated!!
PHP variables can store numeric values as well as string values.PHP can also perform mathematical manipulations like addition, substraction etc. In the code below we're gonna store a numeric value and the add it to another.
<?php
$num = 5;
$sum = $num + 5; // this will give 10
$difference = $num - 2; // this will give 3
$multiplication = $num * 2; // this will give 10
$division = $num / 1; // this will give you 5
?>
We'll be using a lot of PHP variables in these tutorials, so please look at these basic PHP variable rules below.
Rules for PHP variables:
$
sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number character
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive $age
and $AGE
are two different variables according to PHP.
"We'll be using a lot of PHP variables in these tutorials, so please look at these basic PHP variable rules."
The Village Geek
"Alright, alright, alright ..." - Matthew McConaughey
We've learnt enough for one tutorial Betsy, go ahead and get some lemonade and a biscuit, and maybe take a long walk to the park will ya :)
In the next tutorial we'll continue in our quest to get you more familiar with the basics of PHP.
Cape Town, South Africa