Data and Variables in PHP

Thursday, September 6, 2012

data and variables in php

Hi , today i will show you how to write variables in PHP and it's will be in tow parts this part content 5 topics  :

  1. Naming variables
  2. Assigning values to variables
  3. Using constants
  4. Removing variables
  5. Handling errors
OK , what is the variables ? 
it's variables are containers that hold information. First, you give a variable a
name, and then you can store information in it .
For example, you could
name a variable $age and store the number 21 in it. After you store information
in a variable, you can use that variable later in the script .

Naming variables

When you naming a variables you should to put a clear name for it , like $fristName or $email .
but don't use variables name like $var1 , $var2 , $var3 etc... , because it's not useful to understand the code .
The rules for variables naming :-

  1. All variables start with dollar sign  ( $ ) .
  2. Variables can be any length .
  3. Variables names can includes letters, numbers, and underscores only.
  4. Variable names must begin with a letter or an underscore .
  5. Uppercase and lowercase letters are not the same
The following are valid variable names:
$_name
$first_name
$name3
$name_3

The following variable names cause error messages:
$3name
$name?
$first+name
$first.name

The first name is invalid because it doesn’t begin with a letter or an underscore,
as required. The three remaining names are invalid because they contain
characters other than numbers, letters, and underscores.

Assigning values to variables

to store data in variables you can do that by following the variable name with single equal sign ( = ) , like this examples :-

$age = 21;
$price = 20.52;
$temperature = -5;
$name = “Clark Kent”;


You can store the value of one variable in another variable, as shown in the
following statements:-

$name1 = “Sally”;
$name2 = “Susan”;
$favorite_name = $name2;


You can create a variable without storing any information in it. For example,
the following statement creates a variable:
$city = "" ;

Tip :
you can use variable like this , $fNmae.$lNmae .
to see the results : make
$fNmae = "Ahmed";
$lName = "Adm";
echo $fName.$lName;
the result is : AhmedAdm .

and you can use it like this , $fName." ".$lName ;
echo $fName." ".$lName ;

also you can use it like this , "Mr . $fName $lName "
echo  "Mr . $fName $lName ";
the result : Mr . Ahmed Adm

Using constants

Constants are similar to variables. Constants are given names, and values are
stored in them. However, constants are constant; they can’t be changed by
the script. After you set the value for a constant, it stays the same .


Constants are set by using the define statement. The general format is as
follows:
define(“constantname”,”constantvalue”);


For example, to set a constant with the weather, use the following statement:
define(“WEATHER”,”Sunny”);

Removing variables

Simply you can remove the data from variables by using this built in function ( unset ) , you can use it like that :-
unset( $variableName ) ;
after that you can't use it and if you use it , you will see error message ! .

Handling errors


PHP tries to be helpful when problems arise by providing error messages. It
provides the following types of messages:


Error message : You receive this message when the script has a problem
that prevents it from running. The script displays an error message and
stops running. The message contains as much information as possible to
help you identify the problem. The following is a common error message:
Parse error: parse error in c:\test.php on line 6
Often, you receive this error message because you’ve forgotten a semicolon,
a parenthesis, or a curly brace.


Warning message : You receive a warning message when the script sees
a problem but the problem does not prevent the script from running.
Warning messages do not mean the script can’t run; they indicate that
PHP believes something is probably wrong. You should identify the
source of the warning and then decide whether it needs to be fixed. It
usually does. For example, you see the following message if you don’t
include a variable name in the print_r statement — print_r() rather
than print_r($varname).
Warning: print_r() expects at least 1 parameter, 0 given
in d: test1.php on line 9
Because this is a warning, not an error, the script continues to execute
the statements after the print_r statement. However, a warning usually
indicates a more serious problem than a notice. In this case, you need to
fix the problem.

Notice : You receive a notice when PHP sees a condition that may be an
error or may be perfectly okay. One common condition that produces a
notice is echoing variables that don’t exist. Here’s an example of what
you might see in that instance:
Notice: Undefined variable: age in testing.php on line 9

i hope you find that helpful for you , and if you have any thing put it in comments .

Continue Reading...

Start Coding in PHP

Tuesday, August 28, 2012


Hello Guys , tip of today is the airplane fly against wind .
today i will start first real lesson in php tutorial it will be focus on three things :-

  1.  Write statements in PHP 
  2. Adding sections to HTML files
  3. PHP output and documenting 
as i told you i will make things more simple in this php tutorial , so to write a statements in PHP you have just to write a "<?php some statements ?>" , and put them to the www folder in your desk or the public folder in web server , after that it will run , that is all .

OK , and when you have an HTML page you can add PHP sections to that page but that page should end with ( .php ) like filename.php , let see example .
Continue Reading...

Install PHP on Lunix , Mac and Windows

Saturday, August 25, 2012

Install php on Lunix and mac

Hi every one today i'm will start up with in in how to setup your environment for PHP  or web applications it's so easy in Windows and Mac and some difficult in Lunix but still easy and i will show you the easiest way and simple it as i can , you can go to other website for more information , i will keep it easy to you .
Continue Reading...

Power of PHP and why we use PHP

Friday, August 24, 2012

PHP Power
Hi everyone , Today i will start with you about what`s PHP and why we use PHP to developing our web applications and how PHP is easy to learn and understand more that other programming languages .

Continue Reading...

Start with Simple PHP Tutorial

Thursday, August 23, 2012


Hi , i'm Ahmed Adm and i'm good programmer for web apps and in this blog i wanna share my knowledge about php programming , i will try to keep this PHP tutorials easy as never you see before and i will promises you to keep blogging as i can to make you learn PHP   in easy way , and i will use a simple English for people who do not speak  English very well .
this tutorials will be for all beginners and professional programmers  . the main content of tutorials will be like this : -

  1. Say Hello to PHP Scripting Language
  2. Variables and Data in PHP
  3.  Basic PHP Programming
  4. Common PHP Applications
  5. PHP and MySQL Database
  6. Ten Things to DO and DON't 
this general topic and i describe it in separate pages .
i will use this books : 
and use php.net as a mine source to simple lessons to you  , and you can buy this book Simple PHP to get more lessons about PHP, HTML and  JavaScript . 
i hope you to subscribe to our mail list to get content first hand . 
Thank you for reading article and stay connect to get help in PHP programming .


try Blogging to Bank for 1$ for 7 days .
Continue Reading...