I have a code in PHP. However when I echo
, nothing is printed on page.
Can anyone help me?
public function checkcharacter(Request $request)
{
$woord = str_split($request->session()->get('woord'));
foreach ($woord as $letter) {
if ($request->letter === $letter) {
// return view('hangman')->getData (['goed', 'goed gedaan' =>$woord]); // =>$dottedword
return view('hangman')->with (['woord' => $woord, 'correct' => 'fout']);
}
else {
echo "wrong answer!";
}
}
}
If I understand you correctly and you want to get only wrong answer!
string once in the case if the latter isn't the same you need to use break;
construction.
public function checkcharacter(Request $request)
{
$woord = str_split($request->session()->get('woord'));
foreach ($woord as $letter) {
if ($request->letter !== $letter) {
echo "wrong answer!";
break;
}
// return view('hangman')->getData (['goed', 'goed gedaan' =>$woord]); // =>$dottedword
return view('hangman')->with(['woord' => $woord, 'correct' => 'fout']);
}
}
Hope I understand your question in the proper way.
Upd 1.0
If you don't want to break the loop, then I have the second way of resolving your problem:
public function checkcharacter(Request $request)
{
$woord = str_split($request->session()->get('woord'));
$isCorrect = true;
foreach ($woord as $letter) {
if ($request->letter !== $letter) {
$isCorrect = false;
}
// return view('hangman')->getData (['goed', 'goed gedaan' =>$woord]); // =>$dottedword
return view('hangman')->with(['woord' => $woord, 'correct' => 'fout']);
}
if (!$isCorrect) {
echo 'wrong answer!';
}
}
The following else block should be outside of foreach block
else {
echo "wrong answer!";
}
So suppose the if condition is success then the flow will return without any echo. I have not run this snippet. Please check it at your end.
public function checkcharacter(Request $request)
{
$woord = str_split($request->session()->get('woord'));
foreach ($woord as $letter) {
if ($request->letter === $letter) {
// return view('hangman')->getData (['goed', 'goed gedaan' =>$woord]); // =>$dottedword
return view('hangman')->with (['woord' => $woord, 'correct' => 'fout']);
}
}
// if answer is not found then will give the following echo once.
echo "wrong answer!";
}
If you just want to check if the letter is in the word, there is no need for a loop, just check if the letter is in the word - I use strpos()
public function checkcharacter(Request $request)
{
if ( strpos ($request->letter, $request->session()->get('woord')) !== false) {
return view('hangman')->with (['woord' => $woord, 'correct' => 'fout']);
}
else {
return view('hangman')->with (['woord' => $woord, 'correct' => 'false']);
}
}
Note that both branches return a view - the second one where the letter isn't found sets 'correct' => 'false'
which you may need to change to suit your needs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.