indianlibra Posted March 27, 2007 Share Posted March 27, 2007 In the below script you see a string length counter This script is working fine but i have a problem When first time i open this page in my browser it show an error in line 2 (explained in image 2) But when u enter the test string the error removed I can't remove the 'test' variable b'coz i have to work with it Can anybody help me to to remove this erro line in the browser <?php $var = $_GET["test"]; $len = strlen($var); if($len > 0) { echo $len; } else { echo "error: no input"; } ?> <form action="var_test.php" method="GET"> <input type="text" name="test" > <input type="submit" value="Click Here"> Image 1 Image 2 Image 3 Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/ Share on other sites More sharing options...
papaface Posted March 27, 2007 Share Posted March 27, 2007 try: <?php if (isset($_GET["test"])) { $var = $_GET["test"]; $len = strlen($var); echo $len; } ?> <form action="var_test.php" method="GET"> <input type="text" name="test" > <input type="submit" value="Click Here"> Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/#findComment-216256 Share on other sites More sharing options...
wildteen88 Posted March 27, 2007 Share Posted March 27, 2007 It is becuase when your first run the script the test variable is not set in the URI and this PHP displays the Undefined index notice message, but when you submit the form with filled filled in (or not) the test variable in the URI is then set in the URI What you should do is check that the variable exists first before using it. You'd do this by using the isset function in an if statement. Take a look at the above example posted by papaface Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/#findComment-216276 Share on other sites More sharing options...
indianlibra Posted March 28, 2007 Author Share Posted March 28, 2007 try: <?php if (isset($_GET["test"])) { $var = $_GET["test"]; $len = strlen($var); echo $len; } ?> <form action="var_test.php" method="GET"> <input type="text" name="test" > <input type="submit" value="Click Here"> thanks now its without the 'Notice' Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/#findComment-216560 Share on other sites More sharing options...
jitesh Posted March 28, 2007 Share Posted March 28, 2007 <?php if($_GET){ $var = $_GET["test"]; $len = strlen($var); if($len > 0) { echo $len; } else { echo "error: no input"; } } ?> <form action="var_test.php" method="GET"> <input type="text" name="test" > <input type="submit" value="Click Here"> ------------------------------- or <?php error_reporting(0); ............... ............... ?> Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/#findComment-216561 Share on other sites More sharing options...
indianlibra Posted March 28, 2007 Author Share Posted March 28, 2007 yesh i think this 'error reporting(0)' is a goog trick thanks Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/#findComment-216563 Share on other sites More sharing options...
wildteen88 Posted March 28, 2007 Share Posted March 28, 2007 yesh i think this 'error reporting(0)' is a goog trick thanks not its not. Do not ignore errors/messages PHP displays always try to fix them. Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/#findComment-216925 Share on other sites More sharing options...
indianlibra Posted March 28, 2007 Author Share Posted March 28, 2007 yesh i think this 'error reporting(0)' is a goog trick thanks not its not. Do not ignore errors/messages PHP displays always try to fix them. ya thats true I mean by that i just need to avoid the unwanted 'Notice' from my clean page ;) Quote Link to comment https://forums.phpfreaks.com/topic/44530-problem-with-php-script/#findComment-216960 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.