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
?>
Asempty
takes only one argument (manual), you can't use it likeempty($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 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";
}
To check if both strings are empty, do:
if (empty($firstString) && empty($secondString)) {
...
}
Also, you might want to read about logical operators.
Regardingstrcmp
, you just need to flip the function calls -!== 0
means both strings are not equal.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.