JKG Posted August 10, 2011 Share Posted August 10, 2011 anyone know why this wouldn't work? function tws_empty($var, $type){ if(!empty($var) and ($var != '')){ //type 1: profile page address if($type == 1){ echo $var . ', <br />'; } } } the field from the row is definitely empty, but i cant get it to show as empty... even a simply line like: if(empty($displayUser['address_2'])){echo 'asdf';}; just isnt echoing... any thoughts? i know its something really simple!! thanks. Joe. Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/ Share on other sites More sharing options...
tqla Posted August 10, 2011 Share Posted August 10, 2011 Have you tried echoing $displayUser['address_2'] to see what you get? Also, try a test with isset if(isset($displayUser['address_2'])){ echo 'asdf'; } What happens when you try these? Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255590 Share on other sites More sharing options...
JKG Posted August 10, 2011 Author Share Posted August 10, 2011 already tried that, doesnt work. i suppose it is set... any other thoughts? i just thought i was being thick! Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255594 Share on other sites More sharing options...
tqla Posted August 10, 2011 Share Posted August 10, 2011 Change if(!empty($var) and ($var != '')) to if(!empty($var) && ($var != '')) Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255596 Share on other sites More sharing options...
JKG Posted August 11, 2011 Author Share Posted August 11, 2011 thanks for your continued help. unfortunately that to does not work. php does not recognise this variable as empty, even though it is. :s any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255724 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2011 Share Posted August 11, 2011 How do you know the field from the table row is empty? What does the following show - var_dump($displayUser['address_2']); Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255743 Share on other sites More sharing options...
JKG Posted August 11, 2011 Author Share Posted August 11, 2011 i think its empty because i put nothing in it. and phpMyAdmin shows nothing in it. string(1) " " is what is returned... Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255750 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2011 Share Posted August 11, 2011 It's a string consisting of 1 (one) white-space character. It's either a space, a tab, or it could even be a new-line. What does the following show - ord($displayUser['address_2']); Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255755 Share on other sites More sharing options...
JKG Posted August 11, 2011 Author Share Posted August 11, 2011 thank you for your help with this. i have now updated my function to: function tws_empty($var, $type){ if(!empty($var) && ($var != '') && ($var != ' ')){ //type 1: profile page address if($type == 1){ echo $var . ', <br />'; } } } i thought empty() took whitespace into account... ord() displays nothing... with or without the space... Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255762 Share on other sites More sharing options...
TeNDoLLA Posted August 11, 2011 Share Posted August 11, 2011 You could just check against !empty(trim($var)) alone. trim() will get rid of extra white spaces. Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255766 Share on other sites More sharing options...
JKG Posted August 11, 2011 Author Share Posted August 11, 2011 that sounds more efficient, however i get this error: Fatal error: Can't use function return value in write context in /home/theweddi/public_html/scripts/functions.php on line 270 even when im not calling the function. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255768 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2011 Share Posted August 11, 2011 Where is the data value in the table originally coming from? Given that the field is named - address_2, this is likely a second line of their address from a form field or from a csv data import. You are expecting it to be empty, but it clearly is not. You need to filter/trim it before it gets inserted and/or you need to find what is causing it to be non-empty but not an actual usable value to be displayed in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255769 Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2011 Share Posted August 11, 2011 As to the ord() function I suggested. You would need put an echo in front of that. Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255774 Share on other sites More sharing options...
JKG Posted August 11, 2011 Author Share Posted August 11, 2011 i trimmed the values before and now it works fine. the function is just to display the various fields, but some are optional, and i didnt want to but a line break and comma there for users who have not entered their details. it was a simple idea but just took me longer due to my lack of proper debugging. so thank you PFMaBiSmAd and TeNDoLLA. ord returned 32 - it has a space character in it... to my mind this is solved now, thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/244453-field-from-database-table-row-is-empty-but-php-doesnt-recognise-it/#findComment-1255776 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.