Hello so I guess its simple. I need to check if the 2 given strings are empty then compare both strings using strcmp and have it call back to a function that would echo out a statement if they are a match or not. This is what I have
$firstString = "Geek2Geek";
$secondString = "Geezer2Geek";
function sameVar($firstString, $secondString) {
echo "Both strings similar";
}
function diffVar($firstString, $secondString) {
echo "Both strings do not the same";
}
if (empty($firstString)) {
echo "The first string is empty";
} else {
echo "The first string is not empty";
}
if (empty($secondString)) {
echo "The second string is empty";
} else {
echo "The second string is not empty";
}
if (strcmp($firstString, $secondString) !== 0) {
sameVar($firstString, $secondString);
} else {
diffVar($firstString, $secondString);
}
I created an if statement to check if the strings are empty, but I was wondering if there is a way to create one if statement to check if both strings are empty. I tried
if (empty($firstString, $secondString))
but then nothing appears when I reload the page. Also gives me a red dot in VSCode
I guess the second thing would be using strcmp and having it call the functions. I played around with it and deleting the value within $secondString and it still calls the function sameVar
EDIT:
Per the instructions, I had to have an if statement nested within the first if statement if checked that both strings were not empty. I got it done and its pretty much completed. I just need to figure out how to include an else clause at the end which executes if either the $firstString or $secondString contain an empty value.
My other issue is that either function does not echo out the complete sentence. It should echo "Both Geek2Geek and Geezer2Geek do not match" instead I get "Both and do not match"
<?php
$firstString = "Geek2Geek";
$secondString = "Geezer2Geek";
function sameVar() {
echo "Both $firstString and $secondString match";
}
function diffVar() {
echo "Both $firstString and $secondString do not match";
}
if (empty($firstString) && empty($secondString)) {
echo "The Strings are empty";
} else {
echo "The Strings are not empty <br/>";
if (strcmp($firstString, $secondString) === 0) {
sameVar();
} else {
diffVar();
echo "<p>Either the $firstString variable and the $secondString variable does not contain a value so the two strings cannot be compared.</p>";
}
}
// need to include
// else { echo "<p>Either the $firstString variable and the $secondString variable does not contain a value so the two strings cannot be compared. </p>" }
// must be included at the end of the script
?>
As empty
takes only one argument (manual), you can't use it like empty($firstString, $secondString)
.
So, if you wat to check if both string are empty, then your code is:
if (empty($firstString) && empty($secondString)) {
echo 'Both string are empty';
}
Going further, this code
if (strcmp($firstString, $secondString) !== 0) {
sameVar($firstString, $secondString);
} else {
diffVar($firstString, $secondString);
}
must be rewritten as
if (strcmp($firstString, $secondString) === 0) { // === instead of !==
sameVar($firstString, $secondString);
} else {
diffVar($firstString, $secondString);
}
because strcmp
returns 0
only when strings are the same.
As a sidenote - if your function doesn't need arguments, don't pass'em:
if (strcmp($firstString, $secondString) === 0) {
sameVar();
} else {
diffVar();
}
// where `sameVar` and `diffVar` are:
function sameVar() {
echo "Both strings similar";
}
function diffVar() {
echo "Both strings do not the same";
}