PHP best tutorial(part-3.2)
In part 3.1 already you have know the output using echo of a string.
You can use print() instead of echo. Here is the example:
<?php print "Hello World!!! This is my Code on php language."; ?>
** Most of the time the programmers use echo() statement. It is fast. But when you need to debug your code then it is useful to using print() statement. Speacially if you want see any array then i can use print() statement.
Other statement of PHP:
Using this statement you can see a dynamic data as a output. Example:
<?php printf("There are %d article in Nobels Blog",250); ?>
If you want you will use more type specifier you can do it. Example:
<?php $myXam = 2; $myNum = 83.85484513; printf("In %d nd exam i have got %.3f percent marks",$myXam,$myNum); ?>
In 2 nd exam i have got 83.854845q3 percent marks
- Here i use %.3f instead of %f type specifier because i want three digits after the decimal number.
sprintf() statement is same as printf(), Here is the little differenc is that printf() is use for see the output from a browser and sprintf() use for assign a variable. You can also see the output this variable using echo.
<?php $how = sprintf("Here is output: %08.2f", 150.42 / 20); echo $how; ?>
Here you see, first take a variable- $how and i assign a value here. Then i echo it. But if you use printf statement then you dont need to do that. It is useful when you dont want to see the output in browser but you want to use the variable in a specific code.
** if you divide 150.42 with 20 then the output will 7.521. Here how many number you can see? 5 with decimal, and here is a type specifier %08.2f means i want to see the output within 8 and after the decimal it will 2. Now, in our result here you see that (7.521) the other 3 digits was empty. It is fulfilled with zero. Thats why i use a zero before 8.
Here is output: 00007.52