php syntax


I am learning php and I will post some common syntax in this blogpost. you are encouraged to comment anyway.

1. I have found several syntax in php that confuses me, especially with the use of question mark (?) but now I will explain what it uses.

for example when used in
count = $a > $b ? $b : $a; //this code uses the “?”

then the “?” would mean something like this

count = $a > $b ? $b : $a;
is the same as the following code:
if ($a > $b)
count = $b;
else
count = $a;

2. anyway there is a new trick I just found. can you made this kind of decrement number in zigzag kind of fashion using php?

100 99 98 97 96 95 94 93 92 91
81 82 83 84 85 86 87 88 89 90
80 79 78 77 76 75 74 73 72 71
61 62 63 64 65 66 67 68 69 70
60 59 58 57 56 55 54 53 52 51
41 42 43 44 45 46 47 48 49 50
40 39 38 37 36 35 34 33 32 31
21 22 23 24 25 26 27 28 29 30
20 19 18 17 16 15 14 13 12 11
1 2 3 4 5 6 7 8 9 10

well here is the code then.

<?php
 for ($i = 0; $i < 10; $i++) {
 echo implode(" ", ($i % 2 != 0) ?
 array_reverse(array_slice(range(100,1), $i * 10, 10)) :
 array_slice(range(100,1), $i * 10, 10)) . "
 ";
 }

the code above will display the number in zigzag. But I still cannot explain to you what the code does as I am still learning it. I’ll update as soon as I found out how the code work. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s