简体   繁体   English

PHP不检查脚本文件

[英]PHP not checking script file

I have a php file that contains a javascript object (which is in part echoed by php). 我有一个包含javascript对象的php文件(部分被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: 我遇到的问题是,在调用脚本时,javascript会引发错误:

Uncaught SyntaxError: Unexpected token < 

Which basically means php didn't go through the file and print what it should. 从根本上讲,这意味着php没有遍历文件并打印出应有的内容。 It basically left the file untouched and the php code remained there. 它基本上使文件保持不变,而php代码仍然保留在那里。

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. 更新: PHP实际上检查了文件,发现了一些错误,并因此使用html元素回显了它们。 The invalid '<' token comes from a <br/> printed by php and not from the <?php tag itself, like I (dumbly) thought. 无效的“ <”令牌来自php打印的<br/> ,而不是<?php标记本身,就像我(笨拙地)认为的那样。

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. 因为JSON_encode($object)导致以{开头,以}结尾的字符串,所以您的结果会产生混淆... JSON_icons + { ...导致... }{ ... ,这会导致意外的令牌错误。

Sorry guys, my bad. 抱歉,我不好。 PHP does check the file. PHP会检查文件。 The invalid token is actually part of an error message printed by php. 无效令牌实际上是php打印的错误消息的一部分。 Here is what the file.php looks like if accessed through the browser: 这是通过浏览器访问时file.php的外观:

<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 PS:谢谢,@ Sbls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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