sangkrit

Variables, Constants and Data Types


Variables:

Variables are used for storing values having the name of our choice preceded by a dollar sign. It works as a container for storing numeric or text values.

A variable can hold a string, integer or other data type.

Variables can be defined, declared or assigned wherever needed.

A variable can only contain alphanumeric character or underscore.

For Example:

$age1 = 12

$age2 = 14

Preceding lines declare two variable $age1 and $age2. The values assigned to these variables are 12 and 14; it means that they hold that certain value and now you can treat them as they are the values themselves.

Like: echo $age1 is equivalent to echo 12.

Global variables are the variables which are declared by us so as to connect the two scripts. If the same variable is declared as global variable in two scripts then both the scripts will get connected to each other and there will be only one value for the shared variable.

The same variable will be used as one script will include the other script.

Static Variables provide the variable which is not destroyed when a function ends. The value of static variable can be used next time while calling the function.

Superglobals are the predefined variables. They are always present with their values which are available to all your script.

Some Superglobal variables:

$_SERVER: Contains the information about the environment of the web server like file paths, headers, script locations.

$_POST: Contains the post request information i.e., the variables provided to a script through post method.

$_GET: Contains the information on get request.

$_COOKIE: Contains information from the cookies.

$_SESSION: Contains the information on the variables that are currently registered in the session.

$_FILES: Contains variables provided through file uploads.

$_ENV: Contains variables provided through a script as a part of server environment.

Superglobal variables provide a convenient way to access information about script’s environment from server settings to user inputted data.

Constants: A constant cannot change its value during the execution of the program.

It can be defined globally and can be defined with simple data types like as a string or number.

Constants are defined globally.

$ Sign is not used before a constant.

Constants are defined by define function.

Constants cannot be redefined once they are set.

Constants only evaluate scalar values.

Constants are accessed globally.

Constants are used for storing values which you don’t want to change.

PHP constant names are case-sensitive. Usually, constant names use all-uppercase letters, with underscores to separate words within the name

For example:

<?php

   define(“EXAMPLE”,”Your one day income can change     your world”);

   echo EXAMPLE;

  ?>

The output is:-

Your one day income can change your world

 

Predefined Constants

PHP features a large number of built-in, predefined constants holding various values. Some of these are always available, while other constants become available when certain PHP extensions are enabled.

Some of the predefined constants are:

_FILE_ returns the name of the file that the PHP engine is reading currently.

_LINE_ returns the file’s current line number.

DATA TYPES

 

Data Types: There are different data types which are used for storing different extent of memory and are treated differently in a script.

Boolean True or False
Integer Whole number
String Collection of characters
Float or Double Floating point number like 2.324
Array Ordered set of keys and values
NULL Uninitialized variable

 

Strings:

A string can be used directly in a function call or it can be stored in a variable. Variables can store characters and strings.

For example:

<?php

    $lamp_literacy = “Linux Apache MySQL PHP”

    echo “Linux Apache MySQL PHP”

?>

In the above given example, the first string is stored in the variable $lamp_literacy and the second string is used in the echo function and it is not stored. Always save your strings in variables in case you are planning to use them again.

Use double quotes to start and end your string in case if you are inserting variables into your string definitions.

And use single quote to start and end your string if you are not placing variable inside your string.

Concatenation:

Concatenation is used for combining one or more strings and variables together.

A dot (.) is known as concatenation operator.

It is used for combining the two strings i.e. it is used for combining the left hand operand to the right hand operand. As we can see in the given example:

<?php

  $my_string = “Your one day income can change your world”;

   $newline = “<br/>”;

// concatenating the strings together.

  echo $my_string. $newline;

  echo “What can change my world”. $my_string;

?>

 

Variables and text strings are joined together by (.)

This can be done several times.

Concatenation helps to create the dynamic web site faster.

 

How to combine a string and a number:

    <?php

$my_string = “Here we have inserted”.7.”in between the string”;

echo $my_string;

      ?>

It will give the output: Here we have inserted 7 in between the string.


Leave a Reply