Skip to content

Instantly share code, notes, and snippets.

@Kaapiii
Created November 5, 2021 09:13
Show Gist options
  • Save Kaapiii/1f96af1f23b30e4ba593f6432356f6d7 to your computer and use it in GitHub Desktop.
Save Kaapiii/1f96af1f23b30e4ba593f6432356f6d7 to your computer and use it in GitHub Desktop.
<?php
/**
* --------------------------------------------------------------
* Programming exercises datatypes & logical operators function
* --------------------------------------------------------------
*
* Task: solve all tasks by using one of the four loop types. The same loop type can be used for all functions.
*
* Execute file: "php -f basice_exercises.php"
*/
//-------------------------------------
// Comparsions
//-------------------------------------
// Find the errors
function compareValues($a, $b)
{
return (int) $a == (int) $b;
}
//var_dump(compareValues('abv124', 1));
// find the errors and improve the function
function calculateValues(int $a, int $b)
{
return $a + $b;
}
echo '--------calculateValues----------' . PHP_EOL;
//var_dump(calculateValues(1,2)); // -> should be 3
//var_dump(calculateValues(1.5,2.5)); // -> should be 5
//var_dump(calculateValues(-1.5,2.5)); // -> should be 1
// Why is 1 shown and not "true"?
function compareValues2(string $a, string $b)
{
$compare1 = $a == $b;
echo $compare1. PHP_EOL;
}
echo '--------calculateValues2----------' . PHP_EOL;
compareValues2('hallo', 'hallo');
// ----------
// What are the differences between the two comparisons?
// Why is this
function compareValues3($a, $b)
{
$compare1 = $a == $b;
var_dump($compare1);
$compare2 = $a === $b;
var_dump($compare2);
}
echo '--------calculateValues3----------' . PHP_EOL;
//compareValues3('', 0);
//compareValues3('ad', 'd');
//var_dump(compareValues3('ad', 0));
// Why is the second and third function call also true?
// What must be changed to return the return types showed in the comments below?
function isInputEqual($a, $b): bool
{
return $a == (bool) $b;
}
echo '--------compareValues4----------' . PHP_EOL;
//var_dump(isInputEqual('hallo', 'hallo'));
//var_dump(isInputEqual('', false)); // should return false
//var_dump(isInputEqual('hallo', 1)); // should return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment