Click here to Skip to main content
15,907,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Just started developing themes in wordpress and saw this code on index.php -


PHP
<?php if (have_posts()) : ?>

		<?php while (have_posts()) : the_post(); ?>



What is : used for in the above code ? What does it do ?

I searched but couldn't find the answer on the internet.
Posted

Hi friend,

This is very simple when you want to mix html and php but you don't want to use echo or print. Then what you do is encapsulate the html inside a php if-else block or foreach or for block like this

PHP
<?php if (isset($_SESSION['user'])) : ?>
<!-- Some HTML here -->
<?php else : ?>
<!-- Some HTML here -->
<?php endif; ?>


This way instead of using something cumbersome like this :
PHP
<?php <br mode="hold" /?>  if (isset($_SESSION['user'])) {
      echo '<!-- Some HTML here -->';
  else
      echo '<!-- Some HTML here -->';
  }
?>


In this very small example, the code below seems simple but when you start to put real tags and links inside these echo methods, the whole code become dirty and unreadable very soon.

So, it is a better method to use the former way of coding to generate more clean and readable code. It simple has no big effect on code running time etc.

If, this is the answer you need then, please do mark it as solved :)

With Regards
Tushar Srivastava
 
Share this answer
 
Comments
Allwin456 22-Dec-13 8:14am    
I was thinking that its a ternary operator where ? is missing. But I actually came to know that in if-else statement, we can use : in place of { }. Am I right ? Its been 3 months that Im doing PHP coding. I might have misssed some things like this.
Er. Tushar Srivastava 22-Dec-13 8:19am    
Absolutely :) this is what it is used for, to make code clean and beautiful :) Though there maybe some other use but I cannot see any :)
Allwin456 22-Dec-13 8:21am    
Thanks a lot, friend. 5 stars
Er. Tushar Srivastava 22-Dec-13 8:24am    
Thank you very much friend :) I have been in top list of 24 hours for the first time :) Thank you very much
please follow this url and you can solve your problem

http://codex.wordpress.org/Function_Reference/have_posts

thank you

enjoy!!!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900