As I mentioned in the previous posts, I’m using W3Schools to review HTML, CSS, and PHP in order to create some exercises. Here are a few things that I didn’t know about PHP or want to remind myself about.
When sending users to specific pages I often use numbers to indicate which page they should go to. Spambots will frequently put in random junk so I do some tests before I process the request. PHP has some odd behaviour with some of the functions. For example,
// Invalid calculation will return a NaN value
$x = acos(8);
echo is_nan($x); --> 1
echo var_dump($x); --> float(NAN)
echo is_null($x); --> expect 0 but get a blank line, so only works on numbers—not NAN
echo is_finite($x); --> blank line, so only works on numbers—not NAN
echo is_finite("Hello"); --> Warning: is_finite() expects parameter to be a float
echo is_finite(10); --> 1
echo is_infinite(10); --> expect 0 but get a blank line
echo is_infinite(10e1111); --> 1
// $hello not defined
echo var_dump($hello); --> NULL
echo is_nan($hello); --> blank line
echo is_null($hello); --> 1
echo is_finite($hello); --> 1
echo is_infinite($hello); --> expect 0 but get a blank line
echo is_int($hello); --> expect 0 but get a blank line
var_dump(is_numeric($hello)); --> bool(true)
is_numeric($hello); --> expect 0 but get a blank line
I never gave constants much thought when developing web pages, but i’m thinking that they might come in handy when making interactive games. If I access the database and grab a bunch of data that will be displayed but won’t change, it should go into a constant array which is then accessible to any function on the page. Note that both constant arrays and variables are referenced by their name without a $ before the name.
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0]; --> Alfa Romeo
function arrayDisplay() {
foreach (cars as $value) {
echo "$value ";
}
}
arrayDisplay(); --> Alfa Romeo BMW Toyota
If you use a variable array, the array isn’t available to functions.
$cars = array(
"Alfa Romeo",
"BMW",
"Toyota"
);
var_dump($cars); --> array(3) { [0]=> string(10) "Alfa Romeo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }
function arrayDisplay() {
foreach ($cars as $value) {
echo "$value ";
}
}
arrayDisplay(); -- > Warning: Invalid argument supplied for foreach() …