php script problem

Discussion in 'PHP' started by uca, Oct 17, 2005.

  1. #1
    I am trying to modify a simple php script but I can't get the variation to work.

    Can someone take a look please, I am sure it's simple but I know very little about php.

    Thanks!

    The problem is that the first one below doesn't work, while the second one does, what's the reason if any?

    <?
    $imagevar=rand(1, 10);
    if($imagevar==1) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==2) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==3) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==4) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==5) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==6) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==7) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==8) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==9) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    else if($imagevar==10) { print "<img src="http://www.xxx.yyy/zzz.jpg">";}
    ?>


    <?
    $imagevar=rand(1, 4);
    if($imagevar==1) {print "<img src='xxx.jpg'>";}
    else if($imagevar==2) {print "<img src='xxx.jpg'>";}
    else if($imagevar==3) {print "<img src='xxx.jpg'>";}
    else if($imagevar==4) {print "<img src='xxx.jpg'>";}
    ?>

    Thanks again.:)
     
    uca, Oct 17, 2005 IP
  2. Alx

    Alx Member

    Messages:
    94
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2

    You have double "" in your first code, try adding slashes like this
    print "<img src=\"http://www.xxx.yyy/zzz.jpg\">";

    or change to '
     
    Alx, Oct 17, 2005 IP
    uca likes this.
  3. uca

    uca Well-Known Member

    Messages:
    2,242
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    155
    #3
    Thanx Alx!

    I'm not sure if that was the only reason, but I got it to work!
     
    uca, Oct 17, 2005 IP
  4. king_cobra

    king_cobra Peon

    Messages:
    373
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yes that was the only reason. u shd escape double quotes ( " --> \" ) when used inside a string.
     
    king_cobra, Oct 17, 2005 IP
  5. just-4-teens

    just-4-teens Peon

    Messages:
    3,967
    Likes Received:
    168
    Best Answers:
    0
    Trophy Points:
    0
    #5
    or instead of the \" slashes you could use single quotes like <a href='hhh.htm'>
     
    just-4-teens, Oct 17, 2005 IP
  6. king_cobra

    king_cobra Peon

    Messages:
    373
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    but using single quotes will viloate xhtml validation if he is using it. xhtml speicfies that tag parameter values should be enclosed in double quotes.
     
    king_cobra, Oct 18, 2005 IP
  7. Alx

    Alx Member

    Messages:
    94
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #7
    single quotes can be used like this:
    print '<img src="http://www.xxx.yyy/zzz.jpg">';
     
    Alx, Oct 18, 2005 IP
  8. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Might want to try something like this:

    
    $images = array( 1 => 'http://www.xxx.yyy/zzz.jpg',
        2 => 'http://www.xxx.yyy/zzz.jpg',
        3 => 'http://www.xxx.yyy/zzz.jpg',
        4 => 'http://www.xxx.yyy/zzz.jpg',
        5 => 'http://www.xxx.yyy/zzz.jpg',
        6 => 'http://www.xxx.yyy/zzz.jpg',
        7 => 'http://www.xxx.yyy/zzz.jpg',
        8 => 'http://www.xxx.yyy/zzz.jpg',
        9 => 'http://www.xxx.yyy/zzz.jpg',
       10 => 'http://www.xxx.yyy/zzz.jpg' );
    
    $imagevar=rand(1, count($images));
    printf("%s",$images[$imagevar]);
    
    Code (markup):
    This way you just need to add new images into the array.
     
    durango, Oct 18, 2005 IP
    uca likes this.