Hello world

Hello World

2015年12月26日 星期六

[PHP教學] Lesson 2. IF 判斷式


2015/12/26  Lesson 2. IF 判斷式

Review


※ 每行程式結束要打分號 ';' 。

1. $變數(variables)
$a = 100;               // int 100
$b = 'hello',           // string hello
$a = 'hello world!';    // string hello world!
$a = "$b.world",        // string hello.world
$a = '$b, world!';      // string $b, world!

2. 運算子(operator)
$a = 2;          // int 2
$b = 4;          // int 4
$c = $a + $b;    // int 6

$i = 1;          // int 1
$j = $i;         // int 1
$k = $i;         // int 1

/* 根據上方的變數宣告, 以下三行的執行結果會是一樣的  *
 * 當賦值給變數只有增加1時可以省略數字部分          */
$i++;            // int 2
$j = $j + 1;     // int 2
$j+= 1;          // int 2
※補充: 等號的用途
=      // 賦予值
==     // 比較值是否相等
===    // 比較值和型態是否完全相等

3. for迴圈(for loop)
// 輸出: 0123456
for($i = 0; $i <= 6; $i++) {
    echo $i;
}


Practice


Question 1. How to echo '5, 7 ,9,......, 49.' ?

Question 2. How to echo '481216...44' ?

IF 判斷式

$p = '10';
$k = 10;

echo 'If $p equal $k say hi.
'; if($p === $k) { // === for($i = 1;$i<=3; $i++) { echo '型態一樣!'; } echo '很重要所以重複三次'; } else if($p > $k) { // > echo $p.'>'.$k; } else if($p < $k) { // < echo $p.'<'.$k; } else { // else echo $p.'='.$k; }






[PHP教學] Lesson 1. $變數(variables), function, and for迴圈(for loop)


2015/12/25 Lesson 1. $變數(variables), function, and for迴圈(for loop)

<?php
// 2015/12/25 Lesson 1. $變數(variables), function, and for迴圈(for loop)

$love = '<3';

$j = 'Jacinda'; // This is the man's lover.
$k = 'Kevin';
$cash = 12000; // This is all I have in cash.

$b = 'Brandon';
$c = 'Courtney';
$bc = 501;

echo $k . ' ' . $love . ' '. $j . '</br>';

function love($man, $woman, $atm) {
$gift = 499; // This is the product price.
echo $man . ' buy a gift for ' . $woman . ' spends ' . $gift;
echo ' so he only have ' . ($atm - $gift) . ' left.</br>';
}

love($k, $j, $cash);
love($b, $c, $bc);
love('William', $j, 499);


$apple = 10; // Apple farm have 10 apples.
$kevin = $apple; // Kevin owns the apple farm.
$sum = $kevin; // Total apple count equals to the amount of apple that Kevin owns.
$apple_dayborn = 2; // The amount of apple produced daily: 2.
$day = 5; // The days that apple is being produced.

for($i = 1; $i <= $day; $i++) {
$sum+= $apple_dayborn; // $sum = 10 + 2/day
}

// (((((10) +2) +2) +2) +2) +2 = 20
echo $k . ' produce ' . $sum . ' apples.</br>';


for($i = 1; $i <= 5; $i++) {
echo 'love';
}

/* 1. start
$i = 1; // give 1 for $i.

   2. stop
    $i <= 3; // $i <= 3.

   3. do?
    $i++;
*/

?>