Sinisterly
Guess random number - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: Guess random number (/Thread-Guess-random-number)

Pages: 1 2


Guess random number - Vi-Sion - 04-02-2017

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


RE: Guess random number - Pikami - 04-02-2017

(04-02-2017, 01:27 PM)Vi-Sion Wrote: The error i'm having is :  Notice: Undefined index: hasStarted in C:\xampp\htdocs\test\random.php on line 3

Your problem is written in plain english "Undefined index: hasStarted in C:\xampp\htdocs\test\random.php on line 3"
You are trying to access the variable $_SESSION["hasStarted"] but is is not set.
You can check if a variable is set with the method isset()


RE: Guess random number - Vi-Sion - 04-02-2017

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 .


RE: Guess random number - Pikami - 04-02-2017

(04-02-2017, 01:47 PM)Vi-Sion Wrote: well i know but i heard that in php you don't need to assign a value to a variable before using it.
Yes you do.

(04-02-2017, 01:47 PM)Vi-Sion Wrote:
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 .
This quote states that you don't have to declare variables, witch is right.
But you do have to set a value to a variable before using it.


RE: Guess random number - Vi-Sion - 04-02-2017

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


RE: Guess random number - Pikami - 04-02-2017

(04-02-2017, 01:55 PM)Vi-Sion Wrote: 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

Yes. In php an unset variable will always give you false.
But in your code, you are trying to access a variable index, that will always trow you an error if it is unset.


RE: Guess random number - Vi-Sion - 04-02-2017

Thanks for your help i got it, much appreciated.


RE: Guess random number - Pikami - 04-02-2017

(04-02-2017, 02:12 PM)Vi-Sion Wrote: Thanks for your help i got it, much appreciated.

I am glad I helped Blush


RE: Guess random number - Vi-Sion - 04-02-2017

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>



RE: Guess random number - FutileRmnc - 11-09-2017

I recommended to using Gaussian Distribution , which implemented as random number generator.

https://en.wikipedia.org/wiki/Normal_distribution
http://mathworld.wolfram.com/NormalDistribution.html
https://natedenlinger.com/php-random-number-generator-with-normal-distribution-bell-curve/

I hope that help , comrade.