PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | PHP & MySQL Resources | Web Tools
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

PHP Variables

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive, so $Abc is different from $abc.

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Comment

Comment is a part of your PHP code that will not be translated by the PHP engine. You can use it to write documentation of your PHP script describing what the code should do. A comment can span only for one line or span multiple line.

PHP support three kinds of comment tags :

  1. // This is a one line comment
  2. # This is a Unix shell-style comment. It's also a one line comment
  3. /* ..... */ Use this multi line comment if you need to.

* For all PHP code in this website I will use the comment by gray text and result of the code show you with italic character.

Example

<?
$a="Hello PHP ";
// valid
$Var_is-123="World"; // valid
$num=12; // valid
$_num='130 miles'; // valid
$123Start="Go Now"; // invalid, starts with number
?>

PHP supports eight primitive types.

  • boolean : expresses truth value, TRUE or FALSE. Any non zero values and non empty string are also counted as TRUE.
  • integer : round numbers (-5, 0, 10, 2364, ...)
  • float : floating-point number or 'double' (0.9238, 26.0, ...)
  • string : "Hello World", 'PHP MySQL', etc.
  • array
  • object
  • resource ( one example is the return value of mysql_connect() function)
  • NULL

Output a String

Strings are probably what you will use most in your PHP script. To declare a string in PHP you can use double quotes ( " ) or single quotes ( ' ). There are some differences you need to know about using these two.

Example
<?
$today = 'Friday';

echo "Today is $today <br>"; // Result : Today is Friday
echo 'Today is $today <br>'; // Result : Today is $today <br>
echo "Today is".$today."<br>"; // Result : Today is Friday. Use "." between String and Variable
// three lines above. <br> is HTML tag that means to make a line break

?>


Operators

Operators in PHP are using between two variables or between variable and value.

Operator Description Example Result
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4

Assignment Operators

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y

Array

In PHP an array can have numeric key, associative key or both. The value of an array can be of any type. To create an array use the array() language construct.

Example

<?
$x=array(1, "test", 35, 434, "test2", 6);

/*$x has 6 values. They are indexing by the number starting by 0, 1, 2,... and you can output them by using these index number as $x[0], $x[1], $x[2],...*/
echo $x[0]; // Result : 1
echo $x[4]; // Result : test2


$y = array("hello" => "World", 1 => "PHP is easy", 45 => 100);
/* $y has 3 values. They are indexing by strings and number, use => to fix the index and value. */

echo $y['hello']; // Result : World. You can use ' or " for the index using strings in [] signs.
echo $y[45]; // Result : 100

// these lines below shows you can set an array variable by directly.
$z[0]=12;
$z[1]="Hello";
$z[2]=$z[0]."Years";
?>



PHP Superglobals

Superglobals are variables that available anywhere in the program code. They are :

  • $_SERVER
    Variables set by the web server or otherwise directly related to the execution environment of the current script. One useful variable is $_SERVER['REMOTE_ADDR'] which you can use to know you website visitor's IP address

    Example
    <?php
    echo "Your computer IP is ";
    echo $_SERVER['REMOTE_ADDR'];
    ?>


  • $_GET
    Variables provided to the script via HTTP GET. You can supply GET variables into a PHP script by appending the script's URL like this : http://www.phpeasily.com/test.php?a=100
    or set the a form method as method="get"

    Example
    The target file (test.php) when the URL is http://www.phpeasily.com/test.php?a=100

    <?
    $b=$_GET['a']; // make $b receive the variable from $a at GET method.
    echo $b; // Result : 100
    ?>


  • $_POST
    Variables provided to the script via HTTP POST. These comes from a form which set method="post"
  • $_COOKIE
    Variables provided to the script via HTTP cookies.
  • $_FILES
    Variables provided to the script via HTTP post file uploads.
  • $_ENV
    Variables provided to the script via the environment.
  • $_REQUEST
    Variables provided to the script via the GET, POST, and COOKIE input mechanisms, and which therefore cannot be trusted. It's use the appropriate $_POST or $_GET from your script instead of using $_REQUEST so you will always know that a variable comes from POST or GET.
  • $GLOBALS
    Contains a reference to every variable which is currently available within the global scope of the script.

Send Email to Multiple Persons
You can send email to mutiple persons in one time. This tutorial shows you in 1 file with a normal form. When you submit, this file will be sent an email immediately to the target emails from your database.
Make a Number Format
In this tutorial we'll make number format like 0001, 0002, 0003 in stead of 1, 2 ,3. It's very easy to do this for your counter, category or product ID. etc.


PHPsimple.net
[email protected]