Echo Rules / Problems in PHP Language

Echo is used to show output results in PHP. But sometimes you get confuse for solving simple problems. like

How to add Conditional Statement within echo statement?
 It is wrong to use if conditional statement within echo statement. You should use ternary operator within echo statement. like
 echo '<li' . ($sel_subject == $subject['id']) ? 'class="selected"' : '' . '>';  

Can we use echo statement within echo statement?
No we can not use echo statement within echo statement. It is bad logic. 

Can we use echo with double or single quotes?
Yes; you can use  echo with double and single quotes like

 echo 'This is Quality Written Codes Blog';  
Output is : This is Quality Written Codes Blog

 echo "This is Quality Written Codes Blog";  
Output is : This is Quality Written Codes Blog

What is concatenation in echo statement? 
Concatenation is sued to connect two quotes blocks. like 

 echo "My Blog Name is " . "Quality Written Codes";  

" . " dot is used to add concatenation.

How to use backslash within echo?
There are two ways to use backslash within echo tag.

You can use single quote if you want to use backslash; like it

 echo 'This is Quality Written Codes Blog And My Name is \ Samee Ullah Feroz \';  
Its output is
This is Quality Written Codes Blog and My Name is \ Samee Ullah Feroz \

If you use double quotes for echo statement. then you need to remember this solutions.
  1. \/ => /
  2. \" => "
  3. \/\/ => //
 echo "This is \"Quality Written Codes\" Blog and my name is \\ Samee Ullah Feroz \\";  
Its output is 
This is Quality Written Codes Blog and My Name is \ Samee Ullah Feroz \

How to use Variables within and with echo?
You can use different methods for it. like 

 $blog = "Quality Written Codes";  
 echo "$blog";  
 echo '{$blog}'  
 echo $blog;  

Read More about it in our previous post : Display of Variables

Post a Comment

Give your reviews about this blog. Leave your comments. What do you think about this post?

Comments Guidelines:

1) I love to read comments, but please avoid spam.

2) Like this blog, share your insights, and spread the word.

3) You can use basic HTML/xHTML tags.

4) All comments are DoFollow—please use the section professionally.

5) Kindly mention your name or brand below your comment.

6) Constructive suggestions for improvement are always welcome.


Thanks for visiting Samee Articles.

========================================

Best Regards,

Samee U. Feroz

 
Copyright © 2011-2026. Samee Articles - All Rights Reserved
Proudly powered by Blogger