Difference between count() and sizeof() in PHP

difference between count and sizeof

According to PHP documentation, sizeof() is an alias of  count(). While searching on the web, I found and realized that count() is faster and butter than sizeof(). Here is a good description of this on stackoverflow– scroll down to the answer of Gazoris. to understand the difference between count() and sizeof() in php

According to my understanding when sizeof is an alias of count, its code might be looking something like this.

count() Function:

  1. function count($array_variable){
  2. //...........
  3. //code for counting size of array
  4. //...........
  5. return $size;
  6. }

sizeof() Function:

  1. function sizeof($array_variable){
  2. return count($array_variable);
  3. }

The above example code describes that when you are using sizeof() you are also calling the count() via backdoor. So, use of count is better.

Difference Between echo & Print in PHP

I searched for ‘difference between echo and print in php’, but got different answer with different logics. Here is what I understood after reading different blogs, and tutes. Basically both keywords echo and print work same. they give output to the user, but there are some minor differences as follows:

echo print
gives no return value. gives return value i.e. 1(one)
It can’t be used in expression, because it doesn’t return any value. can be used in expression, because it returns value.

For example, if you write echo print 1;

you’ll get the result: 11

It can take multiple parameters.

For example:

echo "This ", "string ", "was ", "made ", "with multiple parameters.";

It can take only one parameter.
It’s faster It’s slower

Pascal Triangle using PHP

We often hear about the pascal triangle while we go for the interview or when we start learning any programming language. Here is a good example of pascal triangle using php language.


pascal triangle

  1. <?php
  3. $lines = 5;
  4. $star_times = 1;
  5. $blank_times = $lines - $star_times;
  6. $pyramid_char= '<span style="color:#000;">*</span>';
  7. $outer_char ='<span style="color:#fff;">*</span>';
  9. for ($i = 1; $i <= $lines; $i++) {
  10. //Write blank line or whatever you like outside(before) the pyramid shape
  11. for ($blank_before = 0; $blank_before < $blank_times; $blank_before++) {
  12. echo $outer_char;
  13. }
  14. //Write the character to shape a pyramid
  15. for ($star = 1; $star <= $star_times; $star++) {
  16. echo $pyramid_char;
  17. }
  18. //Write blank line or whatever you like outside(after) the pyramid shape
  19. for ($blank_after = 0; $blank_after < $blank_times; $blank_after++) {
  20. echo $outer_char;
  21. }
  22. //increase the star and decrease the space
  23. $star_times = $star_times + 2;
  24. $blank_times = $blank_times -1;
  25. echo '<br/>';
  26. }
  28. ?>