php_variables
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| php_variables [2024/10/06 05:10] – created appledog | php_variables [2024/10/06 05:16] (current) – appledog | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| What's a variable and what can you do with it? | What's a variable and what can you do with it? | ||
| - | < | + | First, we already saw we can store something from readLine() in a variable: |
| + | |||
| + | < | ||
| + | <?php | ||
| + | |||
| + | $n = readLine(" | ||
| + | $f = readLine(" | ||
| + | |||
| + | echo " | ||
| + | echo $n . " | ||
| + | echo "Do you want to eat " . $f . "? | ||
| + | |||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | == Operations | ||
| + | We can add variables together. | ||
| + | |||
| + | < | ||
| + | <?php | ||
| + | |||
| + | $a = readLine(" | ||
| + | $b = readLine(" | ||
| + | |||
| + | echo $a . " + " . $b . " = " . $a+$b . " | ||
| + | |||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Testing variables with IF | ||
| + | We can also determine what to do based on what's in a variable. | ||
| + | |||
| + | < | ||
| + | <?php | ||
| + | |||
| + | $name = readLine(" | ||
| + | $fruit = readLine(" | ||
| + | |||
| + | if ($fruit == " | ||
| + | echo " | ||
| + | echo $name . " is bad!\n"; | ||
| + | } else if ($fruit == " | ||
| + | echo " | ||
| + | echo "I also like {$fruit}. Yum!\n"; | ||
| + | } else { | ||
| + | echo " | ||
| + | } | ||
| + | |||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Integers | ||
| + | We can also store numbers and do operations on those numbers. | ||
| + | |||
| + | < | ||
| <?php | <?php | ||
| Line 16: | Line 72: | ||
| ?> | ?> | ||
| </ | </ | ||
| + | |||
php_variables.1728191455.txt.gz · Last modified: 2024/10/06 05:10 by appledog
