Best Forumer,
I would like you to explain how php works, many people think they know something about PHP, but it is often not correct. Let's start with the basics!
Chapter 1: Variables
§ 1: Introduction
Variables can be used to temporarily store data. Such information may consist of numbers (integers) or pieces of text (strings), but can also be true / false data (booleans) or a set of data (array) contain. All we can create in PHP, we can put in a variable.
§ 2: Creating variables
Creating a variable, also called declaration, done by one U.S. dollars sign ($) followed by a random name. With the equal-to (=) sign, we can then assign a value to the variable. Finally, we close the line with a semicolon.
[php]<?php
$text = 'Hello World!';
echo $text;
?>[/php]
This piece of code we declare the variable $text, adding the value "Hello World!" to it. Then we use an echo to the contents of $text to put on screen. The output is as follows:
§ 3: different types of variablesHello World!
As I mentioned earlier, we can actually save everything in a PHP variable. Depending on the content of a variable belongs to a particular type. Some examples:
[php]<?php
$text = 'Hello World!'; // String
$age = 28; // Integer
$price = 190.75; // Float
$check = false; // Boolean
?>[/php]
Here are some examples of different types of variables. The first variable $text we have already seen and contains a string. If you store numbers in one variable, it is an integer, a float if you concern about the numbers have decimal. If a variable includes true or false, you call it a boolean. A type variable that is not in this example is the array. On this variable I will make a separate tutorial.
As we will see a string in PHP is placed between single quotes. In an integer, float or boolean we do not use quotes!
§ 4: Calculating with variables
We have already seen that we can echo variables, but that's not all. Calculating with variables in PHP is something that we will use frequently. Some examples:
[php]<?php
$amout = 10;
$price = 9.95;
$btw = 0.19; // 19%
$subtotal = $amout * $price; // Calculate subtotal
echo 'Subtotal: '.$subtotal.'<br />';
$aBtw = $subtotal * 0.19; // aBtw = After BTW Calculate BTW
$total = $subtotal + $aBtw; // Calculate Total
echo 'Total: '.$total;
?>[/php]
Output:
This was Chapter 1, soon I will go to the next chapter.Subtotal: 99.5
Total: 118.405
Last edited by yearupie; 03-08-2010 at 11:57 AM.
This forum is great!
Is this from W3 Schools?
Oh well nice tut
Thanked and repped
I'm back.
Ok nice one but don't annoy nextgen
I'm back.