When you're learning a programming language you may become overwelmed with the complexity.
I was very overwelmed when I started to learn PHP for the first time over a year ago.
After I grasped how the syntax worked I could learn new methods more easily.
In this article I'll be giving you tutorials on PHP.
Inorder for you to run the PHP scripts you must have a host that supports PHP (atleast 4.1.0).
I highly recommend Crimson Editor for a PHP editor
-- Writing your first PHP program --
Any file that contains PHP must have the extension .php (eg. test.php)
Open Crimson Editor and start a new document (Ctrl + n).
The first thing I'll show you is how to output text using "echo".
To output (or print) text to the browser you can use several statements.
In this article I'll be using the "echo" statement.
Outputting text using PHP is pointless in my opinion but beginners need to start somewhere.
PHP Code:
<?php
echo "This is a string";
?>
The word "echo" tells PHP to output the text between the two quotes to the screen.
The semicolon tells PHP to end the statement (in this case the "echo" statement)
I recommend that you write more "echo" statements to familiarize yourself with the syntax.
I've seen scripts where the coder repeatively uses the "echo" statement for a block of text.
To give you an idea of what I mean imagine copying the "echo" statement and pasting multiple instances of it.
Doing that is unneccesary. In my opinion it makes the code look sloppy.
The way to output a block of text is presented below
PHP Code:
<?php
echo "Hello, my name is Andrew Harmor
I'm 20 years old and I'm attending Waynesburg College
located in Pennsylvania";
?>
-- Variables --
Variables are going to be your friend because you'll be using them a lot.
The naming convention of a variable consists of the dollar sign and almost any name (eg. $header)
A variable cannot start with a number and it cannot have a special character anywhere in the variable name.
PHP Code:
<?php
$name = "Andrew Harmor";
echo $name;
?>
The equals sign assigns the string to the variable. So when you "echo" (output) the variable it'll display
the string that is assigned to that variable.
You may have noticed that the "echo" statement does not contain any quotes.
When dealing with variables encasing them in quotes is not needed.
If you were to output a string with the variable then you'll need to use quotes
PHP Code:
<?php
$name = "Andrew Harmor";
echo "Hello $name";
?>
As you can see variables and the echo statement are pretty much the same.
You can also assign variables to variables.
That may sound confusing but it works the same way as done in the echo statement.
PHP Code:
<?php
$age = "20";
$name = "Andrew Harmor, $age"
echo $name;
?>
The variable $age has the value "20" assigned to it.
The variable $name has the value "Andrew Harmor, 20" assigned to it.
So when you output the value of $name you'll see exactly that.
If you want to output the value of $age and $name by themselves you would do...
PHP Code:
<?php
$age = "20";
$name = "Andrew Harmor"
echo "My name is $name, I'm $age years old";
?>
Having a value respective to the variable name gives you greater control over how you output them.
There are some instances where you have to assign variable(s) to another variable but only do so if it's your only option.