User Tools

Site Tools


php_variables

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
php_variables [2024/10/06 05:10] – created appledogphp_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?
  
-<Code:PHP|abc.php>+First, we already saw we can store something from readLine() in a variable: 
 + 
 +<Code:PHP
 +<?php 
 + 
 + $n = readLine("What is your name? "); 
 + $f = readLine("What is your favourite fruit? "); 
 + 
 + echo "Hello, Mr"; 
 + echo $n . "!\n"; 
 + echo "Do you want to eat " . $f . "?\n"; 
 + 
 +?> 
 +</Code> 
 + 
 +== Operations 
 +We can add variables together. 
 + 
 +<Code:PHP> 
 +<?php 
 + 
 + $a = readLine("a = "); 
 + $b = readLine("b = "); 
 + 
 + echo $a . " + " . $b . " = " . $a+$b . "\n"; 
 + 
 +?> 
 +</Code> 
 + 
 + 
 +== Testing variables with IF 
 +We can also determine what to do based on what's in a variable. 
 + 
 +<Code:PHP> 
 +<?php 
 + 
 + $name = readLine("What is your name? "); 
 + $fruit = readLine("What is your favourite fruit? "); 
 + 
 + if ($fruit == "poo") { 
 + echo "Ewwwwww!\n"; 
 + echo $name . " is bad!\n"; 
 + } else if ($fruit == "banana") { 
 + echo "{$name} is good.\n"; 
 + echo "I also like {$fruit}. Yum!\n"; 
 + } else { 
 + echo "Hello, {$name}.\n"; 
 +
 + 
 +?> 
 +</Code> 
 + 
 + 
 +== Integers 
 +We can also store numbers and do operations on those numbers. 
 + 
 +<Code:PHP>
 <?php <?php
  
Line 16: Line 72:
 ?> ?>
 </Code> </Code>
 +
php_variables.1728191455.txt.gz · Last modified: 2024/10/06 05:10 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki