2

Oh boy! I cant get this to work. Any ideas on what the heck I'm doing wrong? Here's the code.

I'm trying to echo the script but use a php function to get the directory of the js file!!

Any help would be appreicated!!

echo '<script src="<?php get_some_function();?> . /js/main.js"></script>';

I've tried dif scenerios with escaping but cant get this to output correctly.

2
  • echo '<script src="'.get_some_function().'/js/main.js"></script>'; Commented Oct 9, 2013 at 18:31
  • You cannot embed php code inside php code. especially like that. the embedded code will NOT be executed and will treated as part of the surrounding text. Commented Oct 9, 2013 at 18:36

5 Answers 5

5

Since you're already in the PHP context, you can simply concatenate the strings, like so:

echo '<script src="' . get_some_function() . '/js/main.js"></script>';

Using sprintf() looks more cleaner, though:

echo sprintf('<script src="%s/js/main.js"></script>', get_some_function());
Sign up to request clarification or add additional context in comments.

Comments

4

Instead of opening another script tag inside the string, concat the string and echo. The <?php within your string will not be evaluated.

echo '<script src="'. get_some_function() . '/js/main.js"></script>';

1 Comment

Yes all these work!!! thank you everyone above and below this post!! Darn quotes! LOL!!!!
3

Simple string concatenation:

echo '<script src="' . get_some_function() . '/js/main.js"></script>';

Don't forget to properly escape the output of your function!

Comments

3

try doing this:

echo '<script src="'.get_some_function().' /js/main.js"></script>';

or this:

$value = get_some_function();
echo '<script src="'.$value.' /js/main.js"></script>';

Comments

1

Remember that any variable echoed in single quotes ( ' ' ), the value of that variable will be not printed, and if a variable is echoed in double quotes ( " " ) it will be printed.

Similar is true for returned data from a function stored in a varaible. If you are using single quotes, then every php code (variable, or a method call of a class) should be concatenated using dot operator ( . , :P ) . If you are using double quotes, then no need to use . .

Like in all above answers, they have used . to append the php function call, your code may be fine as below also (not tested by me, so you will need to do adjustment) :

$src = get_some_function();
echo "<script src=$src/js/main.js></script>";

But please note that it is a best practice to use single quotes for any kind of html etc echoed in php code, because HTML attributes are using double quotes.

Hope this will help...

2 Comments

This is wrong. Your code will just display the string as plain text. Did you try it? See here.
Thanks for the link. Just updated my answer. Check here now eval.in/53504

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.