Variables, Type casting ,Concatination and Constants in PhP

1. Variables:

$ sign is used for defining the variables in PHP. eg$var_1 =12. Here the datatype is automatically assigned. You don’t have to specify the datatype of a declared variable.

echo is used to output the results.

2. Changing a variable’s data type:

<?php

$a =$b =5.34; //variable declared

echo (($a>4) and ($a>3))."<br/>"; //gives output 1 since it satisfies the condition. && is a conditional operator

settype($a, int); // sets the datatype to int even if the declared variable is of type float

echo "$a"."<br/>"; //displays only 5 since we set the data type to int

echo (int)($b); //displays the int value since $b is typecasted to int .It does not change the real value but it changes the output.

?>

3. Concatenation :

<?php

$var1 = "ask-cloud,"; //string values has to be put inside the quotes

$var2 = "is a blog for learning";

$num1 = 41; //when declaring numbers you don't need the quotes

$num2 = 42;

echo "$var1" . "$var2" ."<br/>"; //concatenates the two variables var1 and var2 using the dot operator.

echo "$num1" . "$num2" ."<br/>";

?>

4. Constants:

In PhP you can define constants. The value of the constant remains unchanged throughout the execution of the program. You can define a constant using the define () function as shown below. It is common to use capital letters for defining the constants. Also avoid using the php reserved keywords such as boolean, echo etc,,,

<?php

define ("MY_NAME", "michael"); //defines the value for a string

echo MY_NAME . "<br/>";

define ("MY_ID" , 1234); //defines the value for an integer.

echo MY_ID . "<br/>";

?>

Other Interesting Blogs

Leave a Comment

Share via
Copy link
Powered by Social Snap