Nine Years of Service
Posts: 581
Threads: 41
Guess random number 04-02-2017, 01:27 PM
#1
this is my first try with php i'm having an error i'm not sure why this is happening hope someone might help .
Spoiler:
Code:
<?php
session_start();
if($_SESSION["hasStarted"]!=true){
$_SESSION["number"]=rand(0,20);
$_SESSION["hasStarted"]=true;
$_SESSION["tries"]=0;
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="random.php">
<input type="text" name="number"/>
<input type="submit" value="submit"/>
</form>
<?php
echo $_SESSION["number"];
if($_POST){
$_SESSION["tries"]++;
if($_POST["number"]==$_SESSION["number"]){
$_SESSION["hasStarted"]=false;
echo $_SESSION["tries"];
?><script>alert(<?php echo $_SESSION["tries"];?>);</script>
<a href="random.php">PLAY AGAIN</a>
<?php
$_SESSION["tries"]=0;
}
else{?>
<script>alert("false");</script>
<?php echo $_SESSION["number"];
}}?>
</body>
</html>
The error i'm having is : Notice: Undefined index: hasStarted in C:\xampp\htdocs\test\random.php on line 3
Everyone should learn how to code, it teaches you how to think.
You don't have to be a Genius to know how to code you just need to be determined.
The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.
•
Nine Years of Service
Posts: 581
Threads: 41
RE: Guess random number 04-02-2017, 01:47 PM
#3
well i know but i heard that in php you don't need to assign a value to a variable before using it.
Code:
In PHP the type of the variable does not need to be declared before use it because types are associated with values rather than variables.
this is quoted from w3resource .
Everyone should learn how to code, it teaches you how to think.
You don't have to be a Genius to know how to code you just need to be determined.
The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.
•
Nine Years of Service
Posts: 581
Threads: 41
RE: Guess random number 04-02-2017, 01:55 PM
#5
i also heard it takes a default value if i didn't give it one so if used as a boolean and didn't give it a value it's default value should be false
Everyone should learn how to code, it teaches you how to think.
You don't have to be a Genius to know how to code you just need to be determined.
The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.
•
Nine Years of Service
Posts: 581
Threads: 41
RE: Guess random number 04-02-2017, 02:12 PM
#7
Thanks for your help i got it, much appreciated.
Everyone should learn how to code, it teaches you how to think.
You don't have to be a Genius to know how to code you just need to be determined.
The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.
•
Nine Years of Service
Posts: 581
Threads: 41
RE: Guess random number 04-02-2017, 08:24 PM
#9
Here's my final code i'll be glad to get comments if there's anything i should fix in my code.
Spoiler:
Code:
<?php
session_start();
if(isset($_SESSION["hasStarted"])==false){
$_SESSION["number"]=rand(0,100);
$_SESSION["hasStarted"]=true;
$_SESSION["tries"]=0;
}
?>
<html>
<head>
</head>
<body>
<div><p>Guess the hidden number between 0 and 100</p>
<form method="post" action="random.php">
<input type="text" name="number"/>
<input type="submit" value="Guess"/>
</form></div>
<?php
if($_POST){
$_SESSION["tries"]++;
if($_POST["number"]==$_SESSION["number"]){
$_SESSION["hasStarted"]=false;
?><script>alert("You've guessed the hidden number after "+<?php echo $_SESSION["tries"];?>+" tries");</script>
<div><a href="random.php">PLAY AGAIN</a></div>
<?php unset($_SESSION["hasStarted"])?>
<?php
$_SESSION["tries"]=0;
}
else{?>
<script><?php if($_SESSION["number"]<$_POST["number"]){?>alert("Hidden number is smaller");<?php }?></script>
<script><?php if($_SESSION["number"]>$_POST["number"]){?>alert("Hidden number is bigger");<?php } ?></script>
<?php }} ?>
</body>
</html>
Everyone should learn how to code, it teaches you how to think.
You don't have to be a Genius to know how to code you just need to be determined.
The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.
•