Vivid Lust Posted January 26, 2008 Share Posted January 26, 2008 When there is no value in the input, the $pixel is displayed, and i dont want it to!! Problem with the IF statement?? ! Please help! Thanks! Code: <?php // variables $rate = "2"; // set image source $img = "http://www.phpfreaks.com/forums/Themes/default/images/on.gif"; // configure image attibutes list($width, $height, $type, $attr) = getimagesize($img); $pixels = $width*$height; // echo values if( $_POST['url'] != "") { echo $pixels; } ?> <form action="index.php" method="post"> <input type="text" name="url"> <input type="submit" value="Go!"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/ Share on other sites More sharing options...
darkfreaks Posted January 26, 2008 Share Posted January 26, 2008 <?php if( $_POST['url'] = "") { }else{ echo $pixels; }?> Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-449875 Share on other sites More sharing options...
phpSensei Posted January 26, 2008 Share Posted January 26, 2008 <?php if( $_POST['url'] = "") { }else{ echo $pixels; }?> You never use one '=' in a condition. Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-449876 Share on other sites More sharing options...
toplay Posted January 26, 2008 Share Posted January 26, 2008 You never use one '=' in a condition. Never is harsh...there are some situations where it's nice to use, however, of course I do agree with you in this "if" condition case it's incorrectly used. To avoid this mistake in the future, code constant values on the left side of the condition. That way PHP will generate an error in case one forgets and uses one equal sign instead of two. Example: if ('value' = $_POST['url']) {} // This will produce an error (good) if ($_POST['url'] = 'value') {} // This will NOT produce an error and will always evaluate to true (bad) Vivid Lust, I'm not sure what you're trying to do with this code and the "url" value (it's not being used). Here is a little better way to go: <?php $strSubmitted = isset($_POST['submitted']) ? $_POST['submitted'] : ''; // Check if form has been submitted first if (!empty($strSubmitted)) { // or can check for specific value: Go! $strUrl = isset($_POST['url']) ? $_POST['url'] : ''; // variables $rate = "2"; // Instead of hard coding this url did you mean to use the url from the form? // i.e. $img = $strUrl $img = "http://www.phpfreaks.com/forums/Themes/default/images/on.gif"; // configure image attibutes list($width, $height, $type, $attr) = getimagesize($img); $pixels = $width*$height; // echo pixels when url value is not empty if (!empty($strUrl)) { echo $pixels; } } ?> <form action="index.php" method="post"> <input type="text" name="url"> <input type="submit" value="Go!" name="submitted"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-449886 Share on other sites More sharing options...
phpSensei Posted January 26, 2008 Share Posted January 26, 2008 I find this hard to believe, it would be just the same if you used '==' Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-449913 Share on other sites More sharing options...
toplay Posted January 26, 2008 Share Posted January 26, 2008 I find this hard to believe, it would be just the same if you used '==' I don't understand your comment/point. Post an example of what you're referring to. Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-449919 Share on other sites More sharing options...
laffin Posted January 26, 2008 Share Posted January 26, 2008 toplays point is that php will stop with an error, cuz ya can't assigned a variable to a constant. thus having the constant on the left hand side of the comparisons. if($val=123) will not generate an error, as $val is assigned 123, and continue with whatever code is in the if statement if(123=$val) will generate an error, giving u a chance to fix it Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-449924 Share on other sites More sharing options...
phpSensei Posted January 26, 2008 Share Posted January 26, 2008 I find this hard to believe, it would be just the same if you used '==' I don't understand your comment/point. Post an example of what you're referring to. whats with all the confusion? why doesnt he just use double '='? Woudl <?php if( $_POST['url'] = "") { }else{ echo $pixels; }?> make a difference with <?php if( $_POST['url'] == "") { }else{ echo $pixels; }?> or <?php if('value' == $_POST['value']) { }else{ echo $pixels; }?> Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-449946 Share on other sites More sharing options...
toplay Posted January 27, 2008 Share Posted January 27, 2008 whats with all the confusion? why doesnt he just use double '='? LOL...yes he can use double equal signs. The confusion is your post with the comment I already indicated. I still don't understand what you meant by: "I find this hard to believe, it would be just the same if you used '=='" What is hard for you to believe? and what would be the same if you used '=='? Why post such a thing? Quote Link to comment https://forums.phpfreaks.com/topic/87931-script-not-working/#findComment-450242 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.