Home   Help Login Register  
Welcome, Guest. Please login or register.

Login with username, password and session length

Search
Free Website WooThemes - Quality Themes, Great Support
Pages: [1]   Go Down
Print
Topic: PHP Web Development: PHP Variables  (Read 1019 times)
« on: August 30, 2007, 01:22:27 PM »
Tech Offline
Newbie

View Profile
*

iTrader: 1
Posts: 183



While working with any language we make use of variables. Variables are used to store values and reuse them in our code. We use different types of variables in our code such as strings (text), integers (numbers), floats (decimal numbers), boolean (true or false) and objects. In PHP we can make use of variable while writing scripts. In this lesson we're going to cover PHP variables.

What is a variable?

A variable is a mean to store values such as strings, integers or decimals so we can easily reuse those values in our code. For example, we can store a string value such as "I Love PHP" or an integer value of 100 into a variable.

PHP Variable Syntax

$var_name = value;

Defining Variable in PHP?

Here is an example of how to declare a variable in PHP.

Some key points to notice:

- Remember to put the $ sign in front of variables when declaring variables in PHP.

- Variable names must start with letters or underscore.

- Variables can’t include characters except letters, numbers or underscore.

PHP variable types?

Unlike Java or C++, PHP doesn't care about primitive types. Any variable, either a string, an integer or a float is declared the same way. PHP converts the types in the code by itself. Here's what I mean.

//an integer variable $var_name = 100;

//an float variable $var_name = 100.00 ?>

PHP Variable type juggling

Like mentioned above, PHP doesn't require variables to declared using primitive types. Therefore, juggling between two types doesn't require use of any special function. We can simply do things like...

//string var $var = "0";

//var is now float $var += 2.5;

//var is now integer $var += 2;

//var is now string $var .= " is the total"; echo $var;

?>

Concatenating variables in PHP?

In PHP we can join two variables by using the dot '.' operator.

$var1 = "I Love PHP"; $var2 = " and Java";

//prints "I Love PHP and Java" echo $var1 . $var2;

$var1 = "1"; $var2 = "2";

//prints "12"; echo $var1 . $var2;

?>

So there you have it, a quick and easy variable lesson in PHP. You can see more examples of PHP variables at http://php-learn-it.com/php_variables.html or go to Learn PHP for more tutorials.

Have fun coding!

Article Source: http://EzineArticles.com/?expert=Gagand ... gh_Tathgar

Share this topic on AskShare this topic on BlinkbitsShare this topic on BlinklistShare this topic on BloglinesShare this topic on BmarksShare this topic on Del.icio.usShare this topic on DiggShare this topic on DzoneShare this topic on FacebookShare this topic on Feed Me LinksShare this topic on FurlShare this topic on GoogleShare this topic on LiveShare this topic on MagnoliaShare this topic on MySpaceShare this topic on NetvouzShare this topic on NewsvineShare this topic on OneviewShare this topic on RedditShare this topic on RojoShare this topic on ScuttleShare this topic on ShadowsShare this topic on SlashdotShare this topic on SpurlShare this topic on SquidooShare this topic on StumbleUponShare this topic on TechnoratiShare this topic on TwitterShare this topic on TipdShare this topic on YahooShare this topic on Google buzz
« Last Edit: December 31, 1969, 05:00:00 PM by Tech » Logged

Pages: [1]   Go Up
Print
Jump to:  

Untitled Document
(c) 2010 HELM, WHM, cPanel, ASP, .Net development, PHP, MySQL, MSSQL Server, SEO support forum
Powered by SMF 1.1.16 | SMF © 2011, Simple Machines | Forum style designed by PixelSlot
Web Hosting - Sitemap - XML Sitemap