Post: [TUTORIAL] PHP - The Basics [/TUTORIAL]
05-10-2011, 12:40 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by 039

Hey guys..

So, secound tutorial coming up.. I was actually wondering why the heck I didn't post this tutorial before the first one about You must login or register to view this content. Anyway - today I'm going to teach YOU the basics of PHP. This includes starting, print out things, declare variables and the if-statement.

So – done deal. Let’s get to it!

The Basics!
How do we start PHP?

When we're creating a PHP-script it's very, very, very important to tell the browser, that this is a PHP-script. This is done by a simple tag:

    
<?php


Now we have let the browser know, that the following code will be straight PHP. If we suddently want to use HTML, CSS, Javascript etc. we'll have to close or PHP first. This is also done by a simple tag:

    
?>


Now we have closed our PHP field, and now we can type pure HTML, CSS etc.

To sum this up:

    
<?php
// Now we have started PHP
// Do something!
// Now please close the PHP again.
?>


How to print our things
Functions to print out: echo(); and print();

Now you've been learning how we start PHP by adding tags to our document. Now - if we want to show some text on our screen, we would be using one of the given functions.

There is a slight difference of using print or echo - print can return a true or false boolean, echo can't. On the other hand, echo do run slightly faster in big application, but for our own sake, lets just stick to what ever you like. It hasn't really much of difference in the beginning. I will be using echo(); in this tutorial.

First of, you shouldn't do the mistake by adding parentheses as print nor echo is a real function - it's simply not needed!

Now - let's add some code.

As you already know, we should start PHP by adding
    
<?php
// Our code
?>


So - lets try to replace "// Our code" with one of the two given functions, echo or print - Lets write "Hello HF!".

    
<?php
echo "Hello HF!";
print "Hello HF (this is printed as well)!";
?>


Now we should see: Hello HF!Hello HF (this is printed as well)!
(Adding new lines could be useful - simply add "<br />" at the end (yet - still inside of our of our echo function).

Declaring variables
In PHP we use a dollarsign ($) to declare variables!

If we would like to create a variable in PHP, we add a dollarsign ($) infront of our variable name.

The most important things about variables is that you can't declare a variables that starts with a space, containing a space or anything else that could have anything with a space to do.

So - if we could like to declare a variables we should do like this:
    
<?php
// start PHP
$thisIsAVariable = "Hello HF!"; // Declare our variable "thisIsAVariable" with the content of "Hello HF!"
echo $thisIsAVariable; // Lets print out our just created variable.
// end PHP
?>


As you may noticed, i didn't add a double quote nor single quote. As we're only printing our variable, we don't have to do this.

Please notice, that if you're adding single quotes around echo and print, you will now have the power of showing the content of a variable. If you by some reason want to use single quotes, you'll have to escape the content by adding '. $thisVar .' - might get back to that later on, for now - just use the double quotes.

The if statement
The functions goes like this: if(expression) { statement }

Everything about PHP is very good, but if we don't know how to put two texts, variables or even expressions up against each other we wont get far.

The structure of the if-statement is shown above, but i'll give an example underneath here:

    
<?php
// start php
$var1 = 1; // Declare the variable "var1" with the content of "1".
$var2 = 2; // Declare the variables "var2" with the content of "2".

// Have in mind, that our if-statement looks like this:
// if(expression) {
// do something
// }

if($var1 == $var2) {
echo "var1 is equal to var2";
}
// end php
?>


Now - as you see, we use the double equal signs which tells us, that the expression has to be true (no matter string-type).
We got two "types" of comparison operators - the double and triple equal (or whatsoever).

Lets just take equal - simple and easy:

Let's picture this code:
We got a variable that contains a simple integer - eg. 3.
Furthermore we got a variable that contains a string - eg. 3 too, but with single qoutes around the integer number.

Now - if we compare our two variables with a double equal, we will get true. If we - on the other hand - uses the triple equal, we will get false as the two variable types are different.

Lets see with some code:

    
<?php
// lets start php

$varInt = 3;
$varString = '3';

if ($varInt == $varString) {
echo "We got a match as we aren't considering the two different types of variables!";
}

// Now lets try with the triple equal:

if ($varInt === $varString) {
echo "Woups! We got no match as we are using two different types of variables - integer and string!";
}

// end php
?>


(For more operators, please visit You must login or register to view this content. OR You must login or register to view this content.)

So, this was just about this. If I've forgot anything, please leave a comment.. Smile


source You must login or register to view this content.
Last edited by CodingNation ; 05-10-2011 at 12:47 AM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo