Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

Webbsrvprg/ovningar/php_grunder/

php_grunder_1.php
php_grunder_2.php
php_grunder_3.php
php_grunder_4.php
php_grunder_5.php

php_grunder_4.php

38 lines ASCII Windows (CRLF)
<?php
// Arrays to store Celsius and Fahrenheit values
$celsiusArray = [];
$fahrenheitArray = [];

// Loop through Celsius values from -20 to 40 in steps of 10
for ($celsius = -20$celsius <= 40$celsius += 10) {
    
$fahrenheit $celsius 32// Convert Celsius to Fahrenheit
    
$celsiusArray[] = $celsius// Store Celsius value in array
    
$fahrenheitArray[] = $fahrenheit// Store Fahrenheit value in array
    
echo "Celsius: $celsius, Fahrenheit: $fahrenheit<br>";
}

// Print arrays using a custom loop
echo "<h3>Using a custom loop:</h3>";
echo 
"Celsius Array: ";
foreach (
$celsiusArray as $value) {
    echo 
"$value ";
}
echo 
"<br>Fahrenheit Array: ";
foreach (
$fahrenheitArray as $value) {
    echo 
"$value ";
}

// Print arrays using var_dump
echo "<h3>Using var_dump:</h3>";
echo 
"Celsius Array: ";
var_dump($celsiusArray);
echo 
"<br>Fahrenheit Array: ";
var_dump($fahrenheitArray);

// Print arrays using print_r
echo "<h3>Using print_r:</h3>";
echo 
"Celsius Array: ";
print_r($celsiusArray);
echo 
"<br>Fahrenheit Array: ";
print_r($fahrenheitArray);
?>