User Tools

Site Tools


php_variables

This is an old revision of the document!


PHP Variables

  • The second Roger/Charles class, on Oct. 14, 2022.

Variables

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("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";

?>

Testing variables with IF

We can also determine what to do based on what's in a variable.

<?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";
	}

?>

Integers

We can also store numbers.

<?php

	$a = 5172;
	$b = 171;

	echo $a * $b;

	echo "\n";
?>
php_variables.1728191633.txt.gz · Last modified: 2024/10/06 05:13 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki