[PHP] Re: isset($a-b) even if $a-b = null
Olav Mørkrid wrote: how do i test if a property of a stdclass object is set, even if its value is null, similar to how array_key_exists() works for arrays. the following method fails: $a-b = null; if(isset($a-b)) echo yes; and property_exists() seems only to work for defined objects. hope someone can help. thanks! You can try: unset($a-b) Or change isset() to empty(). empty() catches more than isset() e.g. '' (empty string), false, 0 etc. are considered empty. Depending on your logic it can still be very useful. It is a language construct rather than a function so it's also efficient. Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: isset($a-b) even if $a-b = null
the solution has been found. array_key_exists() can actually be used on objects, and yields the correct result. http://no.php.net/array_key_exists thanks to dordea cosmin for pointing this out. On 17/08/07, Olav Mørkrid [EMAIL PROTECTED] wrote: the test i need should give the following results: - FALSE when $a-b does not exist at all - TRUE when $a-b = null - TRUE when $a-b = any value empty() gives true for both $a-b = null and not setting any value, so that's no good. borokovs suggestion seems to miss the purpose. anyone else? On 17/08/07, Colin Guthrie [EMAIL PROTECTED] wrote: Olav Mørkrid wrote: how do i test if a property of a stdclass object is set, even if its value is null, similar to how array_key_exists() works for arrays. the following method fails: $a-b = null; if(isset($a-b)) echo yes; and property_exists() seems only to work for defined objects. hope someone can help. thanks! You can try: unset($a-b) Or change isset() to empty(). empty() catches more than isset() e.g. '' (empty string), false, 0 etc. are considered empty. Depending on your logic it can still be very useful. It is a language construct rather than a function so it's also efficient. Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: isset($a-b) even if $a-b = null
Found something. For class variables.. http://us.php.net/manual/en/function.property-exists.php class a { var $b; } if (property_exists('a','b')) { print yes\n; } On 8/17/07, Olav Mørkrid [EMAIL PROTECTED] wrote: the test i need should give the following results: - FALSE when $a-b does not exist at all - TRUE when $a-b = null - TRUE when $a-b = any value empty() gives true for both $a-b = null and not setting any value, so that's no good. borokovs suggestion seems to miss the purpose. anyone else? On 17/08/07, Colin Guthrie [EMAIL PROTECTED] wrote: Olav Mørkrid wrote: how do i test if a property of a stdclass object is set, even if its value is null, similar to how array_key_exists() works for arrays. the following method fails: $a-b = null; if(isset($a-b)) echo yes; and property_exists() seems only to work for defined objects. hope someone can help. thanks! You can try: unset($a-b) Or change isset() to empty(). empty() catches more than isset() e.g. '' (empty string), false, 0 etc. are considered empty. Depending on your logic it can still be very useful. It is a language construct rather than a function so it's also efficient. Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: isset($a-b) even if $a-b = null
yes, but that assumes you have a defined class. if $a comes from mysql_fetch_object() for instance you have just a stdobject, and this method will produce an error. On 17/08/07, Michael Preslar [EMAIL PROTECTED] wrote: Found something. For class variables.. http://us.php.net/manual/en/function.property-exists.php class a { var $b; } if (property_exists('a','b')) { print yes\n; } On 8/17/07, Olav Mørkrid [EMAIL PROTECTED] wrote: the test i need should give the following results: - FALSE when $a-b does not exist at all - TRUE when $a-b = null - TRUE when $a-b = any value empty() gives true for both $a-b = null and not setting any value, so that's no good. borokovs suggestion seems to miss the purpose. anyone else? On 17/08/07, Colin Guthrie [EMAIL PROTECTED] wrote: Olav Mørkrid wrote: how do i test if a property of a stdclass object is set, even if its value is null, similar to how array_key_exists() works for arrays. the following method fails: $a-b = null; if(isset($a-b)) echo yes; and property_exists() seems only to work for defined objects. hope someone can help. thanks! You can try: unset($a-b) Or change isset() to empty(). empty() catches more than isset() e.g. '' (empty string), false, 0 etc. are considered empty. Depending on your logic it can still be very useful. It is a language construct rather than a function so it's also efficient. Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: isset($a-b) even if $a-b = null
the test i need should give the following results: - FALSE when $a-b does not exist at all - TRUE when $a-b = null - TRUE when $a-b = any value empty() gives true for both $a-b = null and not setting any value, so that's no good. borokovs suggestion seems to miss the purpose. anyone else? On 17/08/07, Colin Guthrie [EMAIL PROTECTED] wrote: Olav Mørkrid wrote: how do i test if a property of a stdclass object is set, even if its value is null, similar to how array_key_exists() works for arrays. the following method fails: $a-b = null; if(isset($a-b)) echo yes; and property_exists() seems only to work for defined objects. hope someone can help. thanks! You can try: unset($a-b) Or change isset() to empty(). empty() catches more than isset() e.g. '' (empty string), false, 0 etc. are considered empty. Depending on your logic it can still be very useful. It is a language construct rather than a function so it's also efficient. Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: isset($a-b) even if $a-b = null
Maybe if you tell us exactly what you wish to achieve. Class variables that are not created at object creation is bad design. Olav Mørkrid schreef: yes, but that assumes you have a defined class. if $a comes from mysql_fetch_object() for instance you have just a stdobject, and this method will produce an error. On 17/08/07, Michael Preslar [EMAIL PROTECTED] wrote: Found something. For class variables.. http://us.php.net/manual/en/function.property-exists.php class a { var $b; } if (property_exists('a','b')) { print yes\n; } On 8/17/07, Olav Mørkrid [EMAIL PROTECTED] wrote: the test i need should give the following results: - FALSE when $a-b does not exist at all - TRUE when $a-b = null - TRUE when $a-b = any value empty() gives true for both $a-b = null and not setting any value, so that's no good. borokovs suggestion seems to miss the purpose. anyone else? On 17/08/07, Colin Guthrie [EMAIL PROTECTED] wrote: Olav Mørkrid wrote: how do i test if a property of a stdclass object is set, even if its value is null, similar to how array_key_exists() works for arrays. the following method fails: $a-b = null; if(isset($a-b)) echo yes; and property_exists() seems only to work for defined objects. hope someone can help. thanks! You can try: unset($a-b) Or change isset() to empty(). empty() catches more than isset() e.g. '' (empty string), false, 0 etc. are considered empty. Depending on your logic it can still be very useful. It is a language construct rather than a function so it's also efficient. Col -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php