简体   繁体   中英

PHP not checking script file

I have a php file that contains a javascript object (which is in part echoed by php). That file is called by:

<script src="js/file.php"></script>

The contents of the file are basically:

<?php
header('Content-Type: text/javascript');
$custom_icons = JSON_encode($object);
?>

var JSON_icons = <?php echo $custom_icons ?>;


var icons = JSON_icons + {
    "facebook": {'name': 'facebook', 'text': 'Facebook', 'icon_url': 'img/logos/facebook.png', 'url': 'http://www.facebook.com'}
};

Please ignore any coding / syntax errors that might appear due to the shortening of the script. The problem I'm having is that when the script is called, javascript throws the error:

Uncaught SyntaxError: Unexpected token < 

Which basically means php didn't go through the file and print what it should. It basically left the file untouched and the php code remained there.

How can I solve this? I'm really lost here.

Thank you!

UPDATE: PHP actually checked the file, found some errors, and consequentially echoed them using html elements. The invalid '<' token comes from a <br/> printed by php and not from the <?php tag itself, like I (dumbly) thought.

I think the Problem is this:

var icons = JSON_icons + {
    "facebook": {'name': 'facebook', 'text': 'Facebook', 'icon_url': 'img/logos/facebook.png', 'url': 'http://www.facebook.com'}
};

Because JSON_encode($object) results in a string beginning with { and ending with } your result in concatinating ... JSON_icons + { ... results in ... }{ ... which causes the unexpected token error.

Sorry guys, my bad. PHP does check the file. The invalid token is actually part of an error message printed by php. Here is what the file.php looks like if accessed through the browser:

<br />
<b>Notice</b>:  Undefined variable: icons in <b>C:\...\js\file.php</b> on line <b>42</b><br />

var JSON_icons = null;

...

var icons = custom_icons + {
    "facebook": {'name': 'facebook', 'text': 'Facebook', 'icon_url': 'img/logos/facebook.png', 'url': 'http://www.facebook.com'}, ...
};

PS: Thanks, @Sbls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM