Custom PHP functions, does an empty return string count as false

Hi, I am continuing my PHP learning by investigating writing my own functions in PHP. I seem to understand passing parameters and returning a value. I understand I can set the return value to a string, a value or true or false.

I have a simple function that returns either an empty string or a message. It seems to also act as false if the string is empty ‘’ and true if the string contains characters.

My question is - is it reliable to do this or should I only trust the result returned as true or false if it is actually set to true or false?

depends on what you expect the application to behave like, here is a list of what you can expect from PHP:

https://www.php.net/manual/en/types.comparisons.php

1 Like

Wow! - that is a VERY useful resource for me - clears up a LOT of questions - Thanks!

1 Like

@kerry14,

Try and get used to viewing the free Php online manual because it is the best manual I have ever used.

One section which may be of use is:

https://www.php.net/manual/en/language.functions.php

I also strongly advise searching for the Php declare(strict_types=1); because I think it will soon be compulsory with Php version 8.0 which is due at the end of this month.

Slightly O/T, but I found it quite disappointing that when I searched for a list of significant changes coming in PHP version 8.0, I found that I didn’t use (or understand, for that matter) more of them than I did. Always something to learn.

2 Likes

Never stop learning, and never grow up.
“Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.” -Me.

Also, to get back to the topic; this right here is why the strict comparison operator exists.

Thanks I’ll check that out. I’ll also look into strict_types.

Yet another re-write - I love PHP but … lol

Eh, don’t be so quick to get discouraged.

PHP was developed in 1995;
Version 2 was released 1997;
3.0 was also 1997, but rewrote the parser.
4.0 was 2000
5.0 was 2004
6.0 was 2005 (and dissolved instantly back into 5.4)
It was then 10 years until 7.0 - effectively a full decade between iterations of PHP.
We’ve been riding 7.0 for 6 years.

Also just because there’s a new version doesn’t necessarily mean there’s rewriting of code to do - it’s unlikely that 8.0 would be substantively not backward compatible.

1 Like

Thanks for the feedback, I’m not sure about how

helps in this case because it seems I am trying to avoid a strict type and use it as both boolean or string ie boolean false if string does not exist and use the string if true, since I believe a function can only return 1 parameter.

I fully agree with the sentiment (especially the second part) but sometimes it is frustrating to have to keep re-learning out of necessity rather than learning by choice and selection. :smile:

Posted before I saw your last reply :smile:

A function will only return one thing, but that one thing can be an array or object data type, so may contain multiple values/properties.
I tend to return an explicit false or null on failure, rather than anything that may evaluate to false. As the table shows, there can be ambiguities, depending on what you test and how.

The link supplied in post #4 demonstrates how umpteen different parameters can be returned by prefixing with an ampersand which passes by reference:

<?php
function add_some_extra(&$string)
{
    $string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str;    // outputs 'This is a string, and something extra.'

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.