Variables, Type casting ,Concatination and Constants in PhP
- Last Updated On: May 4, 2013
- By: Scriptcrunch Editorial
$ 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/>";
?>
Scriptcrunch Editorial
Other Interesting Blogs
[50% OFF] Kubernetes Certification Coupons (CKA, CKAD, CKS, KCNA and KCSA)
Looking for CKA coupon and other Kubernetes certification coupon codes? You have come to the right place. Following are the list of
[80% OFF] Linux Foundation Coupon for December 2024
Looking for CKA Coupon and other Linux Foundation certifications coupons? We got you covered. With the latest Linux Foundation Coupon, you can
[65% OFF] Linux Foundation LFCA, LFCS & LFCT Exam Voucher Codes
Linux Foundation has announced up to a $284 discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux