Friday, May 1, 2009

PHP Variable Data Types : Free PHP Tutorial

In previous post i have said that you can change the value of a variable, so the important of variable type is not much in php. But it is important to have knowledge of PHP Variable Data Types. So I have put all important data types here.

PHP supports eight primitive data types as follow.

Scalar Types:

PHP support four scalar types.

1. Boolean : TRUE or FALSE. Any non zero values and non empty string are also counted as TRUE.

2. Integer : Round numbers (e.g. 52, -5248, 8459)

3. Float : Floating-point number or 'Double' (e.g. 0.845875, 510.05)

4. String : "Hello PHP", ' Free Online PHP & MySQL Tutorial', etc

Compound Types:

PHP support two compound types.

1. Array

2. Object

Special Types:

PHP support two special types.

1. Resource

2. NULL

We will discuss about each data types in future.

Friday, March 6, 2009

PHP Tutorial - Constants in PHP

A constant contains data just like variables, but once you have assigned a value to Constants, you can't change it. It means Constants have a single value throughout the program. Thus it is different from PHP Variables. Defining Constants is different then defining Variable in PHP. Constants do not have a $ before their names. By convention, usually constants are uppercase names. See the example...

define("BIRTHDATE", "1/1/1983");

Whenever you wish to reference it within the body of code, you can do so simply by using its name.

echo(BIRTHDATE);

Constants can contain only scalar values (numbers and string). You can access the Constants everywhere in the program as it has global scope. You can use it even in the functions after the have been declared.


Thursday, March 5, 2009

PHP Variable – Assigning and Reassigning Variables in PHP

Assigning Variables

Variable Assignment in PHP is very easy. Just write the variable name and add equal “=” sign and then the expression that you want to assign to the variable name.

Reassigning Variables

In PHP you will be pleased to know that you can reassign the variable value after declaring it. You can change the values of PHP variable even if the assigned values are different data types. For example.

$value = “Value”;

$value = 5;

You can see the first statement contain string and the second one contain numeric value. Thus you can reassign the variable value even their data type is different.


Saturday, February 28, 2009

How to Declare PHP Variables

In PHP, all variable names must start with the character '$'. Like other languages we do not have to declare the variables at the start of the script or program in PHP. You can declare the variable when you want to use before the statement which contain the variable.

In some languages like C, C++, and Java, we must declare the name and datatype of variable before using it. But in PHP, datatype is related with the assigned value so no datatype declaration is necessary.


Thursday, February 26, 2009

Most important Rules for PHP Variables

Here are some most important rules to remember before declaring PHP Variables.

  • A PHP Variable must start with a letter (A-Z, a-z) or underscore (_).

  • It can only contain alpha-numeric characters (A-Z, a-z, 0-9) and underscore (_).

  • PHP Variable name should not contain spaces. If it contains more than one word then it will be separated by underscore ($emp_salary) or capitalization ($empSalary)

  • PHP Variable name can be any length.

  • PHP Variable are case sensitive. Means Uppercase letter PHP Variables are and lowercase Letter PHP Variables are not the same. For example. $stdname and $stdName are not the same Variable.


Wednesday, February 25, 2009

PHP Variable - Introduction


What is Variable?

A
variable is a symbol that stands for a value that may vary in the program.

PHP Variable:

Like all programing language, PHP allows us to store bits of data in variable, and then to access that data by writing the variable name in the program. But here you can find that the PHP variable is relatively different then other languages like C and JAVA. In PHP, we do not need to declare the variable type when declaring the variable. It also means that we can change the data type of a variable in PHP as we change the data that the variable contains.

Tuesday, February 17, 2009

PHP Syntax

A PHP Scripting block (PHP Syntax starts with <?php and ends with ?>. As described in HTML and PHP Integration post you can declare the php code in many ways. Below is the Example.

<?php
echo “Kaushal Patel”
?>

Be careful that each PHP code must end with a semicolon.

Monday, February 16, 2009

How to create PHP Comments

In the last post you learn about PHP and HTML embedding. Now in this post you will be introduce to another important thing of PHP Programming. It is PHP Comments. You can write comments three different ways in php.

The first way of declaring PHP Comments is like C Language.
/*
This is a C Language type PHP Comment
*/


The second way of declaring PHP Comments is like C++ Language.

// This is a C++ type PHP Comment which ends at the end of line


The third way of declaring PHP Comments is like Shell way.
# This is a shell type PHP Comment which ends at the end of line

How PHP is embedded in HTML?

In below example you will see how you can embed PHP in your HTML Script.

<HTML>
<HEAD>Sample PHP Script</HEAD>
<BODY>
<?php
echo "Kaushal Patel is working as SEO";
?>
</BODY>
</HTML>

When the PHP interpreter reached the php tag the server will run the code and send the output to the system. PHP then replaces that PHP code with its output. Thus the output would be as following.

<HTML>
<HEAD>Sample PHP Script</HEAD>
<BODY>
Kaushal Patel is working as SEO
</BODY>
</HTML>

At Last the HTML code is execute the result on your browser.

Sunday, February 15, 2009

How to Define PHP Tags?


Canonical PHP tags

This style is most universally effective and popular PHP tag style. It looks like this:

<?php ?>

It is safe to use this style as some of the other style of php tags may be phased out in the future.

Short-open (SGML-style) tags


This is a shortest option of php tag style. It looks like this:

<? ?>

You must do one of two things to enable PHP to recognize the tags:
· Choose the --enable-short-tags configuration option when you’re building PHP.
· Set the short_open_tag setting in your php.ini file to on. This option must be disabled to parse XML with PHP because the same syntax is used for XML tags.

ASP-style tags


This Style of tags is just like Microsoft’s ASP Style tags. It looks like this:

<% %>

To use ASP style tags you should change the php.ini configuration

HTML script style tags


HTML script style tags is looks like this:

<SCRIPT LANGUAGE=”PHP”> </SCRIPT>

When using this kind of style be careful if you are using lots of javascript then the close tags are fatally ambiguous.