Results 1 to 5 of 5

Thread: Image URL goes outside img function

  1. #1
    Join Date
    Jun 2009
    Posts
    49

    Image URL goes outside img function

    I have a strange problem. I can not quite understand this problem and so need your help. I use a php function to retrieve the url of the user's profile picture. My function is as below, it is slightly modified but the concept is the same.

    PHP Code:
    <?php function profile_image($pid,$type)
    {
    if(
    $type == 'thumb')
    {
    echo
    'http://imageurl.com';
    }
    else
    {
    echo
    'http://imageurl.com';
    }
    }
    ?>
    When I extract the image with one of the 2 that follow depending on exactly where I am:

    PHP Code:
    <img src="<?php profile_image($userid,'thumb'); ?>">
    echo'<img src="'.profile_image($userid,'thumb').'">';
    The thing is that it this works when I use the top of the two, but when I use the bottom it is going outside imageurl <img-tag>.

    Result with the top:

    HTML Code:
    <img src="http://imageurl.com">
    Result with the bottom:

    HTML Code:
    http://imageurl.com<img src="">
    Is there something I do very wrong here? I'm not sure what it could be. I have only basic knowledge about php, so it may be it is just a small file error that I do not know about?

  2. #2
    Join Date
    May 2009
    Posts
    529

    Re: Image URL goes outside img function

    In the bottom case, the function must return the value to achieve the behavior you expect.

    For example:
    PHP Code:
    <?php
        
    function foo () {
            return 
    'bar';
        }
        echo 
    '<img src="' foo () . '" />';
    ?>
    PHP Code:
    <?php
        
    function foo () {
            echo 
    'bar';
        }
    ?>

    <img src="<?php foo (); ?>" />
    I hope you understand what and why this is so.

  3. #3
    Join Date
    Jun 2009
    Posts
    49

    Re: Image URL goes outside img function

    No, that is. What I can figure out is that it is quite wrong to end a function with echo and that the actual return is always going to use in a function. But exactly why this is so, I have some difficulties to find out. Am I totally off the wall here now? I do not really understand what you mean by "value". Do I need to read something more about PHP to understand this basic concepts? If you have any tutorials for me then I am ready to go through with that.

  4. #4
    Join Date
    May 2009
    Posts
    529

    Re: Image URL goes outside img function

    Return function gives a value, you can say (must be printed out manually). Echo just print out what you put in the echo.

    echo:
    PHP Code:
    <?php
    function q() {
            echo 
    5;
    }
    echo (
    q() == 5) ? 'Function returns 5.':'Function returns NOT 5.';
    ?>
    return:
    PHP Code:
    <?php
    function q() {
            return 
    5;
    }
    echo (
    q() == 5) ? 'Function returns 5.:Function returns NOT 5.';
    ?>

  5. #5
    Join Date
    May 2009
    Posts
    637

    Re: Image URL goes outside img function

    Imagine that you have a function that operates with numbers. If you want to use the feature on, without having to print the results, it would be stupid to use the echo function. By using the return you can use the number on the calculations and the like.

    Code:
    function sum($a, $b)
    {
    return ($a + $b);
    }
    echo (2 + sum(3,5));
    It will then print the number 10

Similar Threads

  1. c++ equivalent function to the c-function 'sprintf
    By Dilbert in forum Software Development
    Replies: 6
    Last Post: 13-12-2011, 04:03 PM
  2. Disk Image Burner application not able to get function
    By Kingston-Guy in forum Windows Software
    Replies: 3
    Last Post: 13-08-2010, 05:56 AM
  3. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  4. Replies: 5
    Last Post: 27-02-2010, 07:52 PM
  5. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM

Tags for this Thread

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
  •  
Page generated in 1,713,452,909.95606 seconds with 17 queries