WHAT GIVES? Why can't I echo a function in a variable?

Discussion in 'PHP' started by MrLeN, Aug 14, 2008.

  1. #1
    This works just fine:

    
    echo myfunction();
    
    Code (markup):
    Then why wont this work?

    
    $page = 'http://www.yadayada.com/yada?v='.myfunction().'';
    
    echo $page;
    
    Code (markup):
    I have also tried

    
    $blah = myfunction();
    
    $page = 'http://www.yadayada.com/yada?v='.$blah.'';
    
    echo $page;
    
    Code (markup):
    Still no wanna work. In all cases all that happens is that the function doesn't get echoed at all. It doesn't display an error or anything, it just wont display.

    What gives?
     
    MrLeN, Aug 14, 2008 IP
  2. yleiko

    yleiko Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    does the function return anything, or does it itself echoes things inside
     
    yleiko, Aug 14, 2008 IP
  3. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Can you post the entire code?

    Jay
     
    jayshah, Aug 14, 2008 IP
  4. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #4
    well, I was echoing inside the function -- and I figured that you just can't use a function inside fopen, so what I did is make it not a function, and instead just plain code.

    Then I tried echoing the variable that I wanted and it still does the same thing. So it's not that I can't use a function inside fopen.

    I will explain here is my entire code (has a lot of includes and (edit outs (because I am working on it)), hope you can read between the lines)

    
    <?
    $lines = file($_SERVER['DOCUMENT_ROOT'] . '/videos/'.$_GET['v'].'.txt');
    $line_amount = count($lines);
    #echo '<pre>'; print_r($lines); echo '</pre>';
    
    $perpage = 1;
    
    $p = isset($_GET['p']) ? $_GET['p'] : 1;
    for ($i = (($p * $perpage) - $perpage); $i <= (($perpage * $p) - 1); $i++){
        if($i >= $line_amount){
            break;
        }
        else{
            if($lines[$i] != ''){
    						$current_video_id = $lines[$i];
            }
        }
    }
    ?>
    <? 
    $video_code = $_GET['v'];
    include $_SERVER['DOCUMENT_ROOT'] . '/videos/'.$video_code.'-config.php';
    ?>
    <?
    #echo $current_video_id;
    #GET THE title 
    #$page = 'http://www.youtube.com/watch?v=XLh4-XTnXq8'; 
    $page = 'http://www.youtube.com/watch?v='.$current_video_id.'';
    echo urlencode($page);
    $start = '<title>';
    $end = '<\/title>';
    $fp = fopen($page, 'r' );
    $cont = "";
        while( !feof( $fp ) ) {
            $buf = trim( fgets( $fp, 4096 ) );
            $cont .= $buf;
        }
        preg_match( "/$start(.*)$end/s", $cont, $match );
        $video_title = $match[ 1 ];
    ?>
    
    Code (markup):
    THIS WORKS
    $page = 'http://www.youtube.com/watch?v=XLh4-XTnXq8';

    THIS DOESN'T WORK
    $page = 'http://www.youtube.com/watch?v='.$current_video_id.'';

    Note: echo $current_video_id works! Then why wont it work in my fopen?

    ie: $current_video_id returns XLh4-XTnXq8 -- so why doesn't it work in fopen?

    Note: I know that the fopen can read the variable because it is displaying the intended URL in the error:

    http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXLh4-XTnXq8%0A
    Warning: fopen(): HTTP request failed! HTTP/1.1 400 Bad Request in c:\documents and settings\administrator\my documents\my websites\_practice\www\display.php on line 33

    Warning: fopen(http://www.youtube.com/watch?v=XLh4-XTnXq8 ): failed to open stream: No error in c:\documents and settings\administrator\my documents\my websites\_practice\www\display.php on line 33
     
    MrLeN, Aug 14, 2008 IP
  5. Sleeping Troll

    Sleeping Troll Peon

    Messages:
    217
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm guessing that you cannot call a function from within a string.
     
    Sleeping Troll, Aug 14, 2008 IP
  6. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Blatantly false.

    Please put:
    var_dump($current_video_id);
    Code (markup):
    above the $page variable.

    Jay
     
    jayshah, Aug 14, 2008 IP
  7. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #7
    I FIXED IT!!!!

    $page = trim('http://www.youtube.com/watch?v='.$current_video_id.'');

    :D
     
    MrLeN, Aug 14, 2008 IP