Basic PHP

Basic PHP

The leading webscripting language of today is PHP, short for 'Personal Homepage Preprocessor'. It can do all kinds of thing for you, as easy as finding out what date it is, send emails to an email list, or it can run a whole game for you.

I will teach you the very basics of this programming language to get you started. Please note that you will not master this language in a matter of hours, but having all the basics in one place will hopefully help you in becoming a great webmaster.

PHP can be written in all simple text editors (I use regular notepad for editing, and 'Notepad++' when I need to find line numbers. Okay, let start!

Basic Knowledge

Requirements

PHP requires you to have a PHP enabled web server. Ask your hoster if you can run PHP, or download a free webserver tool. Google it.

A PHP file should be saved as 'filename.php'. 

Outputting 

To make your webserver notice you are going to use PHP code, find the place where you want it to be executed and write:

<?php
?>


All the PHP code you are going to use, should be placed between the PHP open ('<?php') tag and the PHP end ('?>') tag.

Now you know how to open and close PHP, you also want to know how to make it do something. The basic method of making php output something is 'echo'. You use it like this:

<?php
echo "Hello world!";
?>

This would, indeed, output 'Hello world!' to the browser. It is also very handy to notice that PHP can therefore also output raw HTML. Example:

<?php
echo "Hello <b>world!</b>";
?>

This would output 'Hello world!'.

Variables

In PHP you can link information to a so called 'variable'. A veriable is usually in the format '$name = "Peter Griffins"'. So if you'd want to use this in a script, it would look like this:

<?php
$name = "Peter Griffin";
$age = 40;
echo $name." is ".$age." years old.";
?>

This would output 'Peter Griffin is 40 years old.'. Please note that in PHP the method to join two or more pieces together is the full-stop, or '.'. Also note that $age = 40; doesn't need quotes because it isn't a string, but an integer. If you would write it as $age = "40"; it would work, but you wouldn't be able to make PHP do maths with it.

Variables can have indexes. So $name could have more than just 1 string attached to it. Indexes in PHP are used as '[2]' behind a variable. Indexes in PHP start at 0. So you could use it like this:

<?php
$name[0] = "Peter Griffin";
$name[1] = "Stewie Griffin";
echo $name[1]."' dad is named '".$name[0]."'";
?>

This example would output "Stewie Griffin' dad is named 'Peter Griffin'".

Maths

I told you about doing maths with PHP above, I will show you the basics of using maths in PHP. First you'll need a list of the basic commands you can use.

  • / = divide
  • * = multiply
  • + = add up
  • - = substract
  • ^ = involution
  • sqrt() = square root (example: sqrt(144) = 12)

I will show you an example on how to use this.

<?php
$a = 5;
$b = 8;
$c = 3;
$sum = $a+$b+$c; //sum = 5+8+3 = 16 (By the way, this is a php 'comment', it gets ignored by PHP)
echo "The sum of ".$a.", ".$b." and ".$c." is <b>".$sum."</b>.";
?>


This would output 'The sum of 5, 8 and 3 is 16.'. Of course you can also use the other commands available. It would look something like this:

<?php
$a = 80;
$b = 20;
$c = 10;
$answer = $a+$b/100*$c;
echo $answer;
?>

This would run 80+20/100*10, outputting '10'.

Conditions

if()

PHP uses the command 'if()' to check if a statement or condition is true. It could, for example, check if a user is logged in or if a certain number equals another number. After an if statement you would tell PHP what to do, and then you close the if statement. Example:

<?php
$a = 5+4;
$b = 9;
if($a == $b) {
  echo "5+4 equals 9!";
}
?>

As you can see, this would output '5+4 equals 9!' if 5+4 = 9 (Which is does). '==' is a PHP command for 'equals to'. I will list some other forms of checking conditions.

  • != = Other than
  • <  = Smaller than
  • >  = Bigger than
  • <= = Smaller than or Equal to
  • >= = Bigger than or Equal to

So if you'd want to check if variable a is smaller than variable b, you'd use this script:

<?php
$a = 5;
$b = 8;
if($a < $b) {
  echo "5 is smaller than 8!";
}
?>

This would output '5 is smaller than 8!' if the condition 5 < 8 is true.

Elseif

PHP uses 'elseif' to add another check on the if statement. It will first check the if statement, and if it it returns false, it will check the following elseif statement, if that one returns false, it can check the elseif statement after that (if you have one). You would use it like this:

<?php
$a = "This is a string!";
$b = "This is a different string!";
$c = "This is another string!";
$d = "This is a string!";

if($a == $b) { 
  echo "String A and B match.";
} elseif($a == $c) {
  echo "String A and C match.";
} elseif($a == $d) { 
  echo "String A and D match.";
}
?>

This would output 'String A and D match.'.

Else

PHP will execute the commands after 'else' if no conditions return true. You would use it like this:

<?php
$a = 15;
$b = 15.5;
$c = "Haha!";

if($a == $b) {
  echo "Variable A is equal to variable B";
} elseif($a == $c) {
  echo "Variable A is equal to variable C";
} else {
  echo "Variable A is not equal to variable B nor C";
}
?>

This would output 'Variable A is not equal to variable B nor C';

That's it, you've just learned the very basics of PHP! Now you know this you'll have a basic understanding of how PHP scripts work. I highly recommend you to google for some free and simple PHP scripts and edit them. That's how I learned PHP.

 

PHP Logo
PHP Logo

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working