Hot File

PHP: Variables

View: 536    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Forum PHPBB, VBB   Fields: Other

8 point/2 review File has been tested

A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. In PHP all the variables begin with a dollar sign "$" and the value can be assignes using the "=" operator.

Introduction

A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. In PHP all the variables begin with a dollar sign "$" and the value can be assignes using the "=" operator. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP parser to recognize the variable as such.

Another important thing in PHP is that all the statements must end with a semicolon ";". In PHP we needn't have to specify the variable type, as it takes the data type of the assigned value. The contents of a variable can be changed at any time, and so can its type. To declare a variable, you must include it in your script. You can declare a variable and assign it a value in the same statement.

Here is some code creating and assigning values to a couple of variables:

 

<?php
//Commented lines starting with the double 
//forward slash will be ignored by PHP

//First we will declare a few variables
//and assign values to them


$myText = "Have a nice day!"; 
$myNum = 5;
//Note that myText is a string and myNum is numeric. 

//Next we will display these to the user.
 
echo $myText;

//To concatenate strings in PHP, use the '.' (period) operator  
echo "My favourite number is ". $myNum 
?>

 

The output is: Have a nice day! My favourite number is 5.

Case Sensitivity

One thing that causes many problems and take hours of finding mistakes is case sensitivity. PHP is case sensitive. Have a look at the following code:

 

<?php
$myVar = "WebCheatSheet";
$myvar = "PHP tutorial";

echo "$myVar - $myvar"; //outputs "WebCheatSheet - PHP tutorial"
?>

 

PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.

  • PHP variables must start with a letter or underscore "_".
  • PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • Variables with more than one word should be separated with underscores: $my_variable.
  • Variables with more than one word can also be distinguished with capitalization: $myVariable.

One important thing to note if you are coming from another programming language there is no size limit for variables.

Variable References

PHP also allows you to do some neat things with variables. It allows you to create aliases for variables, and it also allows you to have variables whose name is a variable. A variable reference, or alias, is a variable assigned to refer to the same information as another variable. To assign an alias to a variable, you use the reference operator, which is an equals sign followed by an ampersand. The following code snippet outputs 'Have a nice day!' twice:

 

<?php
$firstVar = 'nice day!';            //Assign the value 'nice day!' to $firstVar
$secondVar = &$firstVar;            // Reference $firstVar via $secondVar.
$secondVar = "Have a  $secondVar";  // Alter $secondVar...
echo $firstVar;
echo $secondVar;  
?>

 

Environment Variables

Beyond the variables you declare in your code, PHP has a collection of environment variables, which are system defined variables that are accessible from anywhere inside the PHP code. All of these environment variables are stored by PHP as arrays. Some you can address directly by using the name of the index position as a variable name. Other can only be accessed through their arrays.

Some of the environment variables include:

 

$_SERVER

Contains information about the server and the HTTP connection

 Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).

$_COOKIE

Contains any cookie data sent back to the server from the client.

Indexed by cookie name. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).

$_GET

Contains any information sent to the server as a search string as part of the URL.

Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).

$_POST

Contains any information sent to the server as a POST style posting from a client form.

Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).

$_FILE

Contains information about any uploaded files.

Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated).

$_ENV

Contains information about environmental variables on the server.

Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).

 

The code to use the environment variables will be as follows:

 

<?php
// moderate shortcut
$newVar = $_COOKIE["myFirstCookie"];

// full version
$newVar = $HTTP_COOKIE_VARS["myFirstCookie"];
?>

 

PHP: Variables

PHP: Variables Posted on 15-12-2015  A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. In PHP all the variables begin with a dollar sign "$" and the value can be assignes using the "=" operator. 4/10 536

Comment:

To comment you must be logged in members.

Files with category

  • Input Validation Using Filter Functions with php

    View: 340    Download: 0   Comment: 0   Author: none  

    Input Validation Using Filter Functions with php

    Category: Forum PHPBB, VBB
    Fields: Other

    2.25/2 review
    Filter functions in PHP might not be sexy, but they can improve the stability, security, and even maintainability of your code if you learn how to use them correctly.

  • The Open/Closed Principle in PHP

    View: 217    Download: 0   Comment: 0   Author: none  

    The Open/Closed Principle in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    0/2 review
    As usual, a good way to grasp what’s actually behind the curtain of the principle and how to take advantage of its benefits is by example. In this article I’ll be setting up a few approachable ones in an attempt to unveil the principle’s driving...

  • Dependency Injection with Pimple in PHP

    View: 264    Download: 0   Comment: 0   Author: none  

    Dependency Injection with Pimple in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    1.6666666666667/3 review
    In application development, we try to create independent modules so that we can reuse code in future projects. But, it’s difficult to create completely independent modules which provide useful functionality; their dependencies can cause maintenance...

  • Working with Directories in PHP

    View: 382    Download: 0   Comment: 0   Author: none  

    Working with Directories in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    2.25/2 review
    Let's start with simple listing the contents of a directory. We need three functions to perform this task: opendir(), readdir() and closedir(). The opendir() function takes one parameter, which is the directory we want to read, and returns a...

  • Guide to Email (Text / HTML / Attachments) in PHP

    View: 362    Download: 0   Comment: 0   Author: none  

    Guide to Email (Text / HTML / Attachments) in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    0/2 review
    At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters.

  • Dynamic Image Generation in PHP

    View: 319    Download: 0   Comment: 0   Author: none  

    Dynamic Image Generation in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    0/1 review
    At first, have a look at the following script that creates captcha image. Next, we will consider each step of image generation in details.

  • Form Processing with PHP

    View: 580    Download: 0   Comment: 0   Author: none  

    Form Processing with PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    2.6666666666667/3 review
    Before you can process the information, you need to create an HTML form that will send information to your PHP script. There are two methods for sending data: POST and GET. These two types of sending information are defined in your HTML form...

  • Function in PHP

    View: 475    Download: 0   Comment: 0   Author: none  

    Function in PHP

    Category: Forum PHPBB, VBB
    Fields: Other

    2.5/2 review
    A function is a block of code which can be called from any point in a script after it has been declared. It is basically a compartmentalized PHP script designed to accomplish a single task. Furthermore, code contained within functions is ignored...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd