0

I am making the scripts of my site sign up page so i want to know can i open a php tag in a script i am doing this for more security..

here the code

<script>
//java script here
<?php 
//php goes here
?>
</script> 

and the script in php here's the code

<?php 
//php goes here 
<script>
//java goes here
</script>
?>

will they will work

2
  • What exactly are you trying to achieve? Commented Nov 25, 2013 at 12:11
  • people why don't you understand i asked can i open php tag in javascript tag and vice-verse and i got my answer then why [on hold] Commented Nov 25, 2013 at 14:49

5 Answers 5

9

i want to know can i open a php tag in a script

PHP is entirely evaluated at the server side before its sent to the browser. So that means the first code block will work without any issues. The second code block will fail when PHP parses the code.

What PHP does is scans, parses, evaluates the code between <?php & ?>. It will simply ignore and send the content outside of this block to the browser -- blindly. This means that your <script> tags are sent to the browser, so that the javascript engine is able to work without any problems.

The second block of code will fail when PHP parses the content: <?php .. <script> .. </script> ?>

Sign up to request clarification or add additional context in comments.

1 Comment

i got it but, php tag in script is not working with this code '<script> //java script here <?php //php goes here ?> </script>' can you clear the concept
0

it won't work, if your purpose is to "hide" your javascript logic try obfuscating your javascript code.

for example: http://www.javascriptobfuscator.com/Default.aspx

Comments

0

The answer is "You , you can" echo out Javascript from within PHP. And you can use PHP inside Javascript tags.

But remember, the PHP code is always processed FIRST on the server.

Then the HTML with Javascript is sent to the client.

And then the Javascript is run at the client machine.

Comments

0

As thrustmaster said, PHP is entirely evaluated at the server side.

You can make the second block of code work by echoing the script:

<?php 
// PHP goes here 
echo '<script>';
// JavaScript goes here
echo '</script>';
?>

4 Comments

i got it but, php tag in script is not working with this code '<script> //java script here <?php //php goes here ?> </script>' can you clear the concept
@user3024503 you need to write something thing like echo " <script type=\"text/javascript\"> alert('a testing'); </script> "; ?> i mean everything inside echo statement
just like this can i use jquery ?
@user3024503 of course, in server side php is evaluted like .txt files. after interpreted in server, ie only replacing the <php stuffs with appropriate html, it will send to client browser. the browser will process the javascripts and html.
-1

Yes, you can use PHP code in <script> tags.

Suppose a request has been made to load your index.php file and you have following code in this file :

$demo = "Demo of PHP in Script Tag.";

//

//Some PHP code

//

<script type="text/javascript"> var a = "<?php echo $demo; ?>"; </script>

//

// Some PHP code

//

//End of file.

Now when you check value of javascript variable a, you will find: "Demo of PHP in Script Tag."

For checking value of javascript variable you can either alert it or check using firebug.

and for script tags in php tags you have echo your script like :

<?php echo "<script type="text/javascript">/*script here */</script>"; ?>

Hope this will helps you.

4 Comments

i got it but, php tag in script is not working with this code '<script> //java script here <?php //php goes here ?> </script>' can you clear the concept
<script type="text/javascript"> var a = "<?php echo $demo; ?>"; </script> take above code as an example, scripts tags are in proper way and i have used php between these script tags. Concept behind this is when your php file will executes on server side all php tags will be executed and prepares their results and after that whole generated html will be submitted to your browser and then browser interprets html code. PHP code is working for me, i have tested it before answering you. can you please tell me What you are doing and where you are getting this issue.
if you are trying to use php tags in non-php files then those will not work, because by default apache find only .php files and execute their code.
ok i got it @jitendra khatri

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.