Udjuni.com

udjuni.com search:




PHP Variables


In PHP, unlike other programming languages you can declare a variable or allocate variable storage space without worrying about the type of data that will be stored in it. Thus values are used when data in your script becomes available and can change during the lifetime of that specific script or when passed to another script.

In general PHP variable declaration starts with the character ($) and that can be followed by a combination of numbers, underscore and letters. Note however that, variable names can not have spaces otherwise it is going to give an error. Bellow are examples of valid variable declarations:
    $b;
    $b_small_letter;
    $123_passed_value;
As you may have already read, every PHP statement has to end with a semicolon (;) or otherwise known as instruction terminator

To assign a value to a variable, you simply do the following:

 $first_name = “Mununga”; 
This statement tells the compiler to store the string “Mununga” in the space allocated to $first_name;
To print the content of that variable you would use the following statement:
 echo $first_name 
which in this case, the compiler will interpret as
 echo “Mununga”; 

Testing the data type of a variable


As soon as a value is assigned to the variable, you can test to see what kind of data is stored in it. The reason you may want to test the data type is that because PHP does not define the variable data type, that variable will only assume the data type after a value had been assigned. Under normal circumstances, the variable with a name $first_name would be expected to carry a string of Alfa-numeric value or string, but in PHP there is nothing to prevent anyone from assigning a number to the variable and the only way for you to know what is in it is to test the data type of its value.

To test the variable data type you use the following statement:
 echo gettype($first_name); 

This will return one of the following data type:
  • Integer – if the value in the variable is a whole number

  • Object – if the value in the variable is an instance of a classs

  • String – if the value in the variable is a collection of Alfa-numeric characters

  • Double – if the value in the variable is a floating point number or decimal number

  • Boolean if the value in the variable is either TRUE or FALSE

  • Array – if the values in the variable are an ordered set of keys and values

By default, if the variable is not assigned a value, the statement will return the value NULL, meaning there is nothing in the allocated storage space.

Changing the data type of the variable


There maybe rare cases when you need to change the data type of the variable. Let us say you assigned a variable named river_depth as Double, but somehow you need to embed the value in a string for the readers you would do the following:
        $river_depth = 9.78;
        echo gettype($river_depth);  // displays the data type : Double;
        settype($river_depth, ‘String’); // changes the data type to String
        echo gettype($river_depth); // displays : String

You can also change the data type by a method widely known as casting . This means instead of using the settype keyword, you just put the data type in brackets in front of the variable. This will change the data type of the variable to the type indicated inside the brackets.
For Example, to convert $river_depth to integer you woul do the following:
echo (interger) $river_depth;
if the current value of river_depth is 9.78, this will display 10. thus it will convert the number to the nearest whole number and display that.



RELATED ARTICLES:


Solving linear equations with two variables
In the last posting titled "Basic Linear equations", we covered the basics of solving an equation with one "unknown". There are rules that govern linear equations that we did not cover in the last posting. I will be covering those rules here before we move on to linear equations with two unkn

Exponents of Variables


Of Variables


Java variables and data types
A variable is defined as a named piece of memory that a java program uses to store data. Each of these storage pieces can only store data of a certain type. In other words, if you defined as variable of type integer, that variable can only hold inte

variable


PL/SQL variable declarations and Block structure
As noted in the previous posting introduction to Oracle PL/SQL has many different features and capabilities , such as the use of b

Arrays in PHP
In the world of programming, you normally use two kinds of variables, one is what is called scalar variables which can only store one value at any given time. Another kind of variable is what is called arrays which are indexed storage location that are able to keep multiple values. By

MySQL String functions
MySQL relational Database system has a lot of built-in functions, some of which are used to retrieve and manipulate data, while others are simply used for server administrative purposes. In this posting we will explore how String functions are used in MySQL. String length function

PHP String functions
So far we have only displayed strings directly to the browser without changing the format. We have successfully done that using the PHP echo () command. In addition to the echo () command, PHP also has two other print functions that allow users to format the output. The formatting may come in the fo

arrays


Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed