SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calling function from inside echo

    I have created a function which I think will work:

    PHP Code:
    function myUserPass(){
    if (
    $row['FirstName'] == ""); {
    echo 
    "<img width=11px src='../site_images/Cross.jpg' />";
    }

    I have an echo function going on, and want to call this function from inside it, but I'm not getting something as all it is doing is writing the function name to the website.

    PHP Code:
    echo "Password: [$myUserPass()]"
    In the example above, the calling of the function, which has been reduced considerably for this post is just writing "()" to the website, when I want the image to show.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    function myUserPass(){

    if (
    $row['FirstName'] == ""); {
    return 
    "<img width=11px src='../site_images/Cross.jpg' />";
    }




    Usually best to have your functions return data (in this case a string) and decide to echo it nearer the end of its journey.

    PHP Code:
    echo "Password: " myUserPass() ; 
    That leaves the door open to doing things like this:

    PHP Code:
    $message "<h3>Hello!</h3>";

    // then some stuff

    // then concatenate more info to the message
    $message .= "Password: " myUserPass() ;


    // then finally, in a place in your code that makes more sense to echo stuff out:

    if( $logged_in ){
    echo 
    $message;



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •