Hi, Guest ~ Login or Register

PHP Introduction

PHP Introduction

Listed In PHP and MySQL » Basics — Viewing Full Tutorial
PHP stands for PHP: Hypertext Preprocessor. PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.

PHP files have the extension .php. Only very, very few web hosts do not support PHP, even most Windows hosts support it!

We enclose PHP code inside these tags:

<?PHP
?>




Remember this for later, ok?

We use variables to hold information.
We can set variables like this:

$variable_name = 'Hello world!';

We can then use them later within another variable or a function. A function does pretty much what it says on the tin.

Let's use the most basic function, echo.

echo "Hello Avengex Users!";

This will output: Hello Avengex Users!

You can also echo variables like this...

$lovers = 'Romeo and Juliet.';

echo $lovers;


...or use them like this:

$who = 'Avengex Users!';
echo "Hello $who";


As you have probably noticed by now, each time you set a variable or use a function, you must end that line with a semi-colon. (;)

Next, let's do some simple calculations.

We use these basic mathematical operators:

+ for addition.
- for subtraction.
* for multiplication.
/ for division.

Let's try it:

$sum = 1 + 1;
echo $sum;


Fancy going a bit more advanced?

$sum = 5 * 42 + 10;
echo $sum;


What to do? Use an if/else statement!

We can use if/else statements to make decisions in PHP, and do certain things depending on the answer.

Here's an example:

$weather = "sunny";
if($weather == "sunny"){
echo "Looks like it's going to be a nice day today!";
} else {
echo "It's not going to be sunny today!";
}


PHP checks to see if what is contained inside the brackets is correct/true. If it is, it executes what's inside the first set of curly braces. If it is wrong/false, it skips to the else part, and executes what is inside the latter curly braces.

Full example.

Just to show you what it looks like when it's all pieced together.

<?PHP

$maths = "fun";

if($maths == "fun"){
$sum = 4156 + 5498;
echo "4156 and 5498 added together equals $sum!";
} else {
echo "Maths sucks.";
}

?>



Hope you learnt something from this random waffle.

Feel free to PM me or email me if you have any questions, and don't forget to check out the PHP site at http://www.php.net for lots of info.

Working Beta

  1. The Forums
    These are mostly functional. If you see any weird bugs, post a thread about it and an administrator will do something.
  2. Tutorial Writing
    You can now submit tutorials to the brand new management system.
  3. Tutorials Home
    View tutorials by categories and search for them here.
  4. Shoutbox
    See below. Registered users only!

Register

Newest User

Say hi to rayjo! rayjo joined on Wednesday, 3rd December.

Sponsor

Shoutbox