Thursday, May 19, 2016

Variable and Constant Meeting 3

Now, we are learning on variables and constants in PHP. TPlease pay attention to the scripts below:

<h3>Variables & Constant</h3>
<hr><h4>Employees Data</h4>
<?php
$id = 19200; $name = "Mr. Bean"; $address = "Jl. Godean Gg. II/4";
define("city", "Yogyakarta");
print("ID : $ID<br>");
print("Employees Name : $name<br>");
print("Address : $address<br>");
printf("City : %s<br>",city);
?>
<br><br><hr>
<h4>Calculating A Circle</h4>
<?php
$rad = 10; define("phi",3.14);
printf("Circle radiant : %s cm<br>",$rad);
printf("Area of a aircle : %s cm<sup>2</sup><br>",($rad*$rad*phi));
printf("Circumference of a circle : %s cm<br>", (2*phi*$rad));
?>

As before, save it as two.php and then open the file via browser. And the result should be like this:


For variables we know from the previous lesson that it must be started with $. But for constant we need to use a function "define" with 2 parameters as you can see above. The first parameter is the name of the constant and the second is the value of the constant.

Please pay attention to each line of commands and refer to the result so that you can analyse what a function is for.

As usual, any questions can be addressed via comments and so do the answers. See you in the next lessons.