PHP syntax error - using elseif [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
This is the code that I wrote and it's not working. it's suppose to ask the user a odd number and check is it correct.
Code:
$guess = $_POST['guess'];
$submit = $_POST['submit'];
if(isset($submit)){
if ($guess/2 %0){
echo "the number you are guessing is not odd";
}elseif{
($guess<$rand)
echo "Your Guess is Smaller than the secret number";
}elseif{
($guess>$rand)
echo "Your Guess is bigger than the secret number";
}else{
($guess==$rand)
echo "you guessed currectly. now try anouthe number!";}
else
header("Location: index.php");
exit();}
?>

Can you try this?
You placed the '()' in your elseif wrong.
<?php
$rand = rand(1, 99);
$guess = $_POST['guess'];
$submit = $_POST['submit'];
if(isset($submit))
{
if($guess / 2 % 0)
{
echo "the number you are guessing is not odd";
}
elseif($guess < $rand)
{
echo "Your Guess is Smaller than the secret number";
}
elseif($guess > $rand)
{
echo "Your Guess is bigger than the secret number";
}
elseif($guess == $rand)
{
echo "you guessed currectly. now try anouthe number!";
}
}
else
{
header("Location: index.php");
exit();
}
?>
I have not tested this code, so i need your feedback. Edit: You've confirmed this worked.
I'd like to provide you the manual about elseif:
http://php.net/manual/en/control-structures.elseif.php
Please consider easier/cleaner coding. Personally I like to use ':' instead of '{}', it's less code and easier to read when you use HTML mixed with PHP like:
<?php
$rand = rand(1, 99);
$guess = $_POST['guess'];
$submit = $_POST['submit'];
if(isset($submit)):
if($guess / 2 % 0):
echo "the number you are guessing is not odd";
elseif($guess < $rand):
echo "Your Guess is Smaller than the secret number";
elseif($guess > $rand):
echo "Your Guess is bigger than the secret number";
elseif($guess == $rand):
echo "you guessed currectly. now try anouthe number!";
else:
header("Location: index.php");
exit();
endif;
?>
And don't forget to check the $_POST data.
Same goes for array, but thats a side note:
$arr = array(1 => 'hi', 2 => 'hello'); // old
$arr = [1 => 'hi', 2 => 'hello']; // new

That's not the correct syntax for an if-else construct in php.
The elseif part needs to have a condition right after it (before the opening brace), while else doesn't expect a condition at all.
if ($guess/2 %0){
echo "the number you are guessing is not odd";
} elseif ($guess<$rand) {
// ....
} else {
echo "you guessed currectly. now try anouthe number!";
}
Of course, before your else you have to be sure that the if and elseifs match all the "wrong" cases.

Related

Adding a Variable with Changing Values to an Array PHP

I am making a number guessing game, where the person guesses different numbers until they guess the correct number, I want to record these different guesses into an array using the same variable but it only records the most recent guess, is there a way so that it records all guesses in this array?
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$_SESSION['incorrect'] = array();
if(isset($_POST['submit'])){
if(1 <= $_POST['guess'] && $_POST['guess'] <= 100){
$guess = $_POST['guess'];
if($guess < $number){
echo "Your Number is Too Low";
$_SESSION['guesses']++;
array_push($_SESSION['incorrect'], $guess);
} elseif($guess > $number){
echo "Your Number is Too High";
$_SESSION['guesses']++;
array_push($_SESSION['incorrect'], $guess);
} else {
echo "Good Job <br>";
echo $_SESSION['guesses']. "<br>";
print_r($_SESSION['incorrect']);
}
}else {
echo "Please Enter a Number Between 1 and 100";
}
}
I think the problem is you’re setting the incorrect array to an empty array every time the page is loaded.
Fix it by changing the part where you set the incorrect array to:
if (!isset($_SESSION[“incorrect”])){
$_SESSION[“incorrect”] = array();
}

Need help in $_GET

I have a php page like this
if($_GET){
if($_GET['name'] && $_GET['number']){
$name = $_GET['name'];
$number = (int) $_GET['number'];
echo "Got it!";
} else {
echo "Please give input!";
}
} else {
echo "Please give input!";
}
Now it works fine when there is no input is given. But if i give one input like page.php?name=Something&number=0 it gives error Undefined index: number
And the value of number also can be 0.
This error doesn't look good. Now how can I get rid from this? Please help!
use isset to check
also check it explicitly like $_GET['name']!='' and dont print or store whatever you get from get and post etc.. if you are storing to database using mysli_real_escape_string. and if you are outputting to browser use htmlspecialchars
if(isset($_GET)){
if(isset($_GET['name']) && $_GET['name']!='' && isset($_GET['number']) && $_GET['number']!=''){
$name = $_GET['name'];
$number = (int) $_GET['number'];
echo "Got it!";
} else {
echo "Please give input!";
}
} else {
echo "Please give input!";

Else If Not Work in PDO Script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I wanted to thwart POST if the number of characters entered is less than 6 use this code:
<?php
if(isset($_POST['button']))
{
$id = $_SESSION['user_session'];
$u_password = $_POST['password'];
if($crud->updatePassword($id,$u_password))
{
$msg = $msgsucces;
}
else if($u_password < 6) {
$msg = "<div class='alert alert-warning'><strong>Failed!!</strong> Minimum length 6 character</div>";
return false;
}
else
{
$msg = $msgfailled;
}
}
echo $msg ;
?>
But apparently it did not work. What is wrong?
You can use your conditions as like that:
$msg = "";
if(strlen($u_password) < 6) {
$msg = "<div class='alert alert-warning'><strong>Failed!!</strong> Minimum length 6 character</div>";
}
else{
if($crud->updatePassword($id,$u_password))
{
$msg = "success message";
}
else {
$msg = "failure message";
}
}
echo $msg;
For checking length of an input you can use strlen() function.
Side note:
Note that I have remove return false from ist condition.
Also suggest you to always add error_reporting() in your file this will help you to save your time. (Only for local environment not for production).
with $u_password <6 you are not really checking length.
You need strlen function to get the length and compare to 6 so we use strlen function
Rule of thumb is you should have wrong cases before the write one.
The last else is executed if $crud->updatePassword($id,$u_password) fails ie false.
if(strlen($u_password) < 6)
{
$msg = "<div class='alert alert-warning'><strong>Failed!!</strong> Minimum length 6 character</div>";
return false;
}
else if($crud->updatePassword($id,$u_password))
{
$msg = $msgsucces;
}
else {
$msg =$msgfailled;
}
}
echo $msg ;
?>

PHP-How to check if the textbox contains one word or two?

Now I want to check if this text box contains one word or two, for example
if ($_POST['mytext'] == two words){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
and I tried
if ($_POST['mytext'] > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
and it didn't work
that what I mean so how to make it?
Hope to find a way to do that.
Thanks
If you define two words as "some characters followed by one space followed by some characters" then you can do something like:
$mytext = $_POST["mytext"];
$parts = explode(" ", $mytext);
if (count($parts) !== 2) {
throw new Exception("too many or too little!");
}
if (strlen($parts[0]) === 0 || strlen($parts[1]) === 0) {
throw new Exception("not enough characters!");
}
Keep in mind that this allows a string like "# !"
Use str_word_count():
if (str_word_count($_POST['mytext']) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
you could use the
`substr_count('some text', ' ');
it will return the number of space,.
try this
$text= preg_split(" ",$_POST['mytext']);
if (count($text) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}

PHP array not outputting properly

I am trying to do form validation but when I try to print out the contents of an array when there is an error it doesnt output anything.
$errors = array();
if (strlen($password) >= 6) {
array_push($errors, "Your password is not long enough! Must be over 6 characters!");
}
if(count($errors) !== 0) {
...
} else {
echo "There is errors<br/>";
foreach($errors as $er){
echo $er . "<br/>";
}
}
What I do get is "There is errors" so I know that the if else is working.
I just have to correct the argument of the if:
if(count($errors) === 0) {
// everything is okay
} else {
echo "There are errors<br/>";
foreach($errors as $er){
echo $er . "<br/>";
}
}
In this way, when your error count is 0, the content of the if is executed. When it isn't 0, the content of the else is executed and the errors are printed. It's just the opposite of what you did.
(I also corrected the sentence: it's ‘there are errors’, not ‘there is errors’ :P)
Furthermore, the other if is wrong as well, it should be the opposite:
if (strlen($password) <= 6) {
since you need to check when the password is less than 6 characters.
Shouldn't it be:
if (strlen($password) < 6) {
array_push($errors, ...);
?
BTW you should use at least constants instead of magic numbers, e.g.
define('MIN_PASSWORD_LENGTH', 6);
// ...
if (strlen($password) < MIN_PASSWORD_LENGTH) {
array_push($errors, "Your password is not long enough!"
. " Must be over ".MIN_PASSWORD_LENGTH." characters!");
}
This way, if your minimal required length changes, you just have to change it once.
Your if statement is messed up. You are checking for errors, then doing nothing, then the else is where it is displaying the errors. Try this:
if(count($errors) >0) { //there are errors
echo "There is errors<br/>";
foreach($errors as $er){
echo $er . "<br/>";
}
}else{
//there are no errors
}
Also, your password length should be <=6 not greater than or equal to if it is too short.

Categories