knowledgediarybd.com

Your Virtual Knowledge Campus







PHP Variables And Strings

 

php variables,php stringsPHP Variables are an essential element in php programming . An efficient way for storing data or information  in  the content of the PHP code are done by using Variable.

PHP variable are also use in  condition checking, looping and  branching.

Dear learner in this tutorial  i discussed details about PHP variables.  I Hope everybody must be benefited  form this tutorial.

Some Major Rules for PHP Variables:

1.  A PHP variable begin with a  dollar sign ($).

2. Variable name must begin with a letter or a underscore ( _ ) character.

3. Variable names  in php  are case sensitive that is  ( A and  a are two different variables ).

4. Variable name can not contain a space.

5. Variable name can contain Numeric ,Characters and  underscore (0-9 , A-Z, a-z ,  _  ).

PHP has total eight kinds of  data types  that is used in  php variable :

1. Integers: are complete numbers  For Example: 65667

2. Strings:   are set of character or word: For Example: ‘PHP support string variable.’

3. Double: are floating  point number  Such as: 3.1416

4. Null:   is a special data type in php that has only one value: Null

5. Booleans: have only two values ‘true’ or ‘false‘.

6.  Resources: are special type of variables that holds references to resources external to PHP.

7. Object: are instance  of developer-defined classes.

8. Arrays: are  indexed  and named collections of other values.

Use of  PHP Variables:

1.  Integers:

They are complete numbers  For Example: 65667 . These  are the simplest type .They can be positive or negative number . Integers can be assigned to a variables  or they can also  be used in expressions. For Example:

$int_1stvariable = 121212;$second_int = -2222+2222;

2. Strings:

Strings are set of character or word: For Example: ‘PHP support string variable.’

Example of  php Strings variable:

$string_var1 = “this is a string in double quotation”;
$string_var2 = “my name is, Mr. X”;
$string_var31 = “this string has thirty-one characters”;
$string_null = “”; // a string with zero/null charactersAnother example of  php Strings variable:<?php
$text1=”Hello World!”;
$text2=”Have  a nice day.”;
echo $text1 . ” ” . $text2;
?>Output:Hello World! Have  a nice day.

 strlen() function:

Purpose: In php strlen () function is used to return the length of a string.

For example:

<?php
echo strlen (“Hello world”);
?>

The output of the  above code is :

11

strpos() function:

Purpose:  In php strpos() function is used for searching

text/character within a string.

For example:

< ?php
echo strpos(“Well come to PHP!”,”PHP”);
?>

Output of the  above  code :
4

3. Double:

They like 4.99999 or 2653.251

For example:

$first_num = 3.50;
$second_num = 35.21;
$ sum= $first_num + $second_num;
print(.$first_num + $second_num = $sum);

4. Boolean:

They have only two values ‘true’ or ‘false‘.

For example:

if (true)
print(“Wel come to boolean data type “);else
print(“Wel come to php programming”);

5. NULL:

    Null  is a special kind of data type in php that has only one value:NULL.

For example:

$null_variable = NULL;

 Variable Scope in PHP:

Variable Scope can be defined as the range of availability in a program in which it is declared.

PHP variables can be  declared  as :

1. Global variables

For Example:

 <?php
$t1 = 5;
$t2 = 10;  // here t1 & t2 are global variablesfunction total()
{
global $t1, $t2;
$sum = $t1 + $t2;
}total();
echo $sum;
?>

2. Local variables.

For Example:

<?php$roll = 21; // global scopefunction myroll()
{
echo $roll; // local scope}myroll();?>

For Example:

3. Static variables. 

For Example: static $victorday;

4. Function Parameters.

For Example:

 function myfunction ($num1,$num2,…)
{
// function code
}

Related Post:

knowledgediarybd.com © 2017
shares