I got a simple Question. I use the expression <?=a_function();?> very often in my system but each and every time it shows "1" on my page when a_function() returns true. What else can I do? I heard I should always return something but whats this with this 1?
            Asked
            
        
        
            Active
            
        
            Viewed 1,477 times
        
    0
            
            
         
    
    
        Stef
        
- 151
- 1
- 9
- 
                    = is perfectly valid shorthand for echoing. – David Wyly May 07 '15 at 21:18
- 
                    1Please see [this post](http://stackoverflow.com/questions/12139200/get-status-function-returns-1-instead-of-true-or-false-why) - "1" is how php converts true to string. – wahwahwah May 07 '15 at 21:18
2 Answers
4
            
            
        1 or 0 is a boolean, which is equivalent to true or false.
How does true/false work in PHP?
Also, if you just want to run the function without outputting anything, just use <?php a_function(); ?>
 
    
    
        Community
        
- 1
- 1
 
    
    
        David Wyly
        
- 1,671
- 1
- 11
- 19
- 
                    what if i want to use =?> without getting 1 in return? shall i create a function withour return true? – Stef May 07 '15 at 21:51
- 
                    No. `= a_function(); ?>` is the direct equivalent of writing ``. The `=` is supposed to be a shorthand alternative for echoing whatever was returned. Use ` – David Wyly May 07 '15 at 21:58
- 
                    
- 
                    It's good practice to return either a value, `true`, or `false`/`null` from a function. If you're not returning anything, returning `true` or a value designates that the function was successful; returning `false` or `null` designates that something within the function did not properly validate. – David Wyly May 07 '15 at 22:16
- 
                    
- 
                    Long story short, if the function is successful, you want to return either the value you're returning or `true`. If the function is not successful, returning `false` is best practice. PHP is a loosely typed language, so it's good to be able to consistently use the `===` operator to check for a return of `false`. This will help avoid issues where a return of `null` or `0` could issue a false-positive during validation checks using the `==` operator. I suggest this thread: http://stackoverflow.com/questions/9643080/what-is-the-php-best-practice-for-using-functions-that-return-true-or-false – David Wyly May 11 '15 at 15:19
1
            
            
        What is the point of what you're doing?
I have'nt understand it,but I think the a_function(); returns the integer value of a boolean expression.
a boolean expression is always 1 or 0 that 1=true and 0=false.
For Example,in PHP if you type 1==0;,It will return false and if you echo it as an integer,it'll print 0.
also,you are using = in you're code.delete it and you're page will show nothing.
 
    
    
        marc_s
        
- 455
- 1
- 4
- 15
- 
                    for instance `a_function($message);` loads a message from the database and "echos" it. if i return true it'll show the message and a 1.. – Stef May 09 '15 at 08:17