Develop PHP Website for Learning Web Design Free Online
  Home   |   Forums   |   DevCMS
  Join   |   Log In
 
   

PHP Tutorials - Learn PHP

Tutor at Develop PHP
Creating Variables in PHP
By: Adam      Created: Jul 14, 2009      Views: 5689 Tweet This Page  Post page to Facebook  Post page to Facebook

Variables are used to make processes we script out more dynamic. In the programming world the word variable means "a symbolic name associated with a value and whose associated value may be changed".

PHP has loose data typing, which means we do not have to claim a data type for a variable unless it is explicitly called for. Which usually it is not called for.

DO NOT use a number to start a variable name!

This functionality allows values to be passed around more easily, changed, and even taken through user input as in a web form field. Variables should be used in your scripts when the value of certain information would be able to vary, change, or be transfered.

PHP variables are made by placing a dollar sign [ $ ], and then the name of the variable directly after it. The variable name is case-sensitive. Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. When you create a variable name it should identify the value or purpose in a common sense way without being too long.

It is best to always initialize your variables before using them, because variables that are not initialized have a default value of their type depending on the context in which they are used. And it can also lead to script errors if certain variables are not initialized before a script runs.

Variables should have names that identify them well when you scan your code. Common sense variable naming helps you and others decipher your code after you write it.

Here are examples of valid variable names with prefilled dummy values, and display them on a web page:
Learn HTML
<?php 

// Setting variable names and values upon creation, names cannot start with a number
$age = 37;
$name "Joe";
$_car_color "green";

// Now echo the sentence to display in the web browser
echo "My name is $name. I am $age years old and my car is $_car_color.";

?>

Develop PHP browser display window
My name is Joe. I am 37 years old and my car is green.


Initialize variable names and give empty values
Learn HTML
<?php 

// Setting variables with empty values
$d4 "";
$name1 "";

?>


Simply initialize the variables
Learn HTML
<?php 

// Simply initialize the variable names
$d4;
$name1;

?>




                                   Constants and Magic Constants in PHP


Comment on Creating Variables in PHP




Search Tags:
Creating   ·  Variables   ·  in   ·  PHP   ·  
 
 
Arbitrary Links and Archives
Home
Active Forums
Members
SIte News
Link To Us
Gear
2009 Forum Archive
Programming Courses
Learn HTML
Learn CSS
Learn PHP
Learn MySQL
Learn Javascript
Learn jQuery
Learn ActionScript 3.0
Learn Java
Learn XML
DevelopPHP Requires Flash Player
Get Adobe Flash player
___________________________________________

Terms of Use  •  Privacy  •  Admin Notes

©2010 developphp.com   |   Powered By FlashBuildingHolder