PHP best tutorial for beginners level to advance (part-3.2)

PHP best tutorial(part-3.2)

PHP best tutorial for beginners level to advance (part-3.2)

PHP best tutorial for beginners level to advance (part-3.2)

echo() statement:

In part 3.1 already you have know the output using echo of a string.

print() statement:

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:

printf() statement:

Using this statement you can see a dynamic data as a output. Example:

<?php

printf("There are %d article in Nobels Blog",250);

?>

 Output:
There is 250 article in Nobels Blog
Here %d is a type specifier, When printf() statement execute then this type specifier %d replace with 250. There is many type specifier, Such as %s, %f, %o etc. If you need to know more you can brows php manual.

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);

?>

Output:

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:

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.

Example:

<?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.

Output:

Here is output: 00007.52

email
(Visited 28 times, 1 visits today)
Sadat Nobel
Blogger at Nobels Blog
This is Sadat Nobel from South Korea. I am tech lover. My blog ( nobelsblog.com) is the leading website that focuses on publishing high quality relevant content in a wide variety technology subjects that people are interested in.
Sadat Nobel on EmailSadat Nobel on FacebookSadat Nobel on GoogleSadat Nobel on Twitter
%d bloggers like this: