0

I am trying to access an item from the array, the issue is with the line:

src=$videoArray[0]

I have tried a few ways but none seem to work.

<?php
$videoArray = array(
"//www.youtube.com/embed/nEBHkEeH42Y",
"//www.youtube.com/embed/1GlticqrECU",
"//www.youtube.com/embed/BMOUsI8JIaI",
);
?>



<iframe width="520" height="280" src=$videoArray[0] frameborder="0" allowfullscreen></iframe>
1
  • 1
    make it src="<?php echo $videoArray[0]; ?>" You can't access PHP variables from HTML like that. Commented Feb 28, 2014 at 22:03

4 Answers 4

3

You need <?php ?> tags for the array to echo out, and you are missing the quotes around the src attribute.

<iframe width="520" height="280" src="<?php echo $videoArray[0]; ?>" frameborder="0" allowfullscreen></iframe>
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot your PHP tags and echo statement:

<iframe width="520" height="280" src="<?php echo $videoArray[0]; ?>" frameborder="0" allowfullscreen></iframe>

or shorthand syntax:

<iframe width="520" height="280" src="<?= $videoArray[0]; ?>" frameborder="0" allowfullscreen></iframe>

8 Comments

do not use short tags
@keaner the right comment is, "don't use short tags if of you plan to distribute your software and support php versions prior to 5.4".
no John, Dont use short tags, ever. Future-proof :)
That thinking is obsolete. Short tags for echoing (<?=) is always enabled as of php 5.4. It is the future.
"PHP also allows for short open tags <? and ?> (which are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. " I agree with the echo part, but as a general practice you should not use short tags.
|
0
<iframe width="520" height="280" src="<?php $videoArray[0] ?>" frameborder="0" allowfullscreen></iframe>

Comments

0

Maybe need reusable code...

<?php
$videoArray = array(
    "//www.youtube.com/embed/nEBHkEeH42Y",
    "//www.youtube.com/embed/1GlticqrECU",
    "//www.youtube.com/embed/BMOUsI8JIaI",
);

foreach($videoArray as $videoLink) {
    ?>
    <iframe width="520" height="280" src="<?php echo $videoLink; ?>" frameborder="0" allowfullscreen></iframe>
    <?php
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.