jeff5656 Posted April 6, 2009 Share Posted April 6, 2009 Let's assume $pt_name has a certain value: if ($pt_name != ""){ $came == "This came from consult list"; } But when I echo $came it is blank. If $pt_name has a value (in this case lets say "johnson") why is $came blank? Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/ Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 'cause using the == operator is testing the value .. use just a single = when assigning. ie., $came = "This came from consult list"; Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802792 Share on other sites More sharing options...
jeff5656 Posted April 6, 2009 Author Share Posted April 6, 2009 if ($pt_name !== ""){ $came = "This came from consult list"; } else { $came = ""; } Thanks that works but now even if pt_name is blank, I get $came = "This came from consult list"; Why is it not going to the else statement even when pt is blank? Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802804 Share on other sites More sharing options...
thepip3r Posted April 6, 2009 Share Posted April 6, 2009 trim $pt_name to ensure that there's no " " -- or blank space entry which is causing the var to have data, it's just a blank character. Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802806 Share on other sites More sharing options...
jeff5656 Posted April 6, 2009 Author Share Posted April 6, 2009 No there were no blank spaces. I type " and then immediately typed another " and it still does not work. When I echo $pt_name it is blank. Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802818 Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 if (!empty($pt_name)){ $came = "This came from consult list"; } Use empty instead. Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802826 Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 you're not telling it what to do .. for instance, are you asking for the string length to be checked? or if the string is set or not? or if it's empty? any one of the following will suffice .. isset() is most common. if (isset ($pt_name)) { $came = "This came from consult list"; } else { $came = "not set"; } or... if (strlen($pt_name) > 0) { $came = "This came from consult list"; } else { $came = "no value present"; } Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802831 Share on other sites More sharing options...
jeff5656 Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks the !empty solution worked. To mrMarcus: After that code, later on I have this statement: <textarea name="comments" id="comments" cols="50" rows="4" ><?php echo $came;?></textarea> Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802834 Share on other sites More sharing options...
mrMarcus Posted April 6, 2009 Share Posted April 6, 2009 Thanks the !empty solution worked. To mrMarcus: After that code, later on I have this statement: <textarea name="comments" id="comments" cols="50" rows="4" ><?php echo $came;?></textarea> what's wrong with the textarea? Link to comment https://forums.phpfreaks.com/topic/152870-assigning-a-value-in-if-statement-is-not-working/#findComment-802847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.