在< head>中编写PHP [英] Writing PHP in <head>

查看:119
本文介绍了在< head>中编写PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将PHP变量/值存储在< head> 标签中是否有效?以这种格式编写HTML与HTML有什么缺点或优点?它甚至是重要的吗?



我通过直接输入进行采样。 orgrel =nofollow> W3C Validator

 <!DOCTYPE html> 
< html>
< head>
< title> PHP头部测试< / title>
<?php $ data; ?>
< / head>
< / html>

我得到1个错误:

 第5行,第4列:Saw <?。可能的原因:尝试在HTML中使用XML处理指令。 (HTML中不支持XML处理指令。)
<?php $ data; ?>

但我猜这是因为它正在验证 .html 其中包含 php 代码的文件。



我计划使用PHP将预处理的MySQL数据存储到要使用的变量中在Javascript中。 < head> 是否存储这些值的适当位置?



注意 :我不想在头上打印数据,那很愚蠢。我正在询问如何在< head> 中存储从MySQL到PHP的预处理数据(用于Javascript)。

不,将PHP变量存储在头部并不是正确的方法。实际上,当你这样做时,你的变量不会被存储在头部,PHP是服务器端的,HTML / CSS / Javascript是客户端。



您的变量,甚至有任何HTML输出。



然而,如果你做这样的事情:

 < HEAD> 
< title> PHP头部测试< / title>
<?php $ data; ?>
< / head>

实际上并没有做任何事情,如果你想显示它,你可以使用echo。这段代码除了声明$ data之外完全没有任何功能,前提是没有声明它。



通常,您应该将大部分PHP代码放在HTML文件之外,他们应该是完全不同的文件,PHP代码应该包含HTML文件。在这种情况下,您放入HTML文件的PHP代码将可以访问文件中包含的所有PHP变量。


Is it valid to store PHP variables/values inside the <head> tag? Are there any disadvantages or advantages to writing PHP with HTML in this format? Does it even matter?

I sampled the following code as direct input through the W3C Validator:

<!DOCTYPE html> 
<html>
<head>
  <title>PHP Head Test</title>
  <?php $data; ?>
</head>
</html>

And I get 1 error:

Line 5, Column 4: Saw <?. Probable cause: Attempt to use an XML processing instruction in HTML. (XML processing instructions are not supported in HTML.)
<?php $data; ?>

But I'm guessing that's because it's validating an .html file that has php code in it.

I am planning to use PHP to store preprocessed MySQL data into a variable to use in Javascript. Is the <head> the proper place to store these values?

NOTE: I am not trying to print out data in the head, that would be silly. I am asking about storing preprocessed data from MySQL to PHP (to be used in Javascript) in the <head>.

解决方案

No, storing PHP variables in the head is not the proper way to do it. In fact, when you do this, your variables are NOT stored inside the head, PHP is server side, HTML/CSS/Javascript are client side.

You want to store your variables before there is even any HTML that is outputted.

However, if you do something like this :

<head>
    <title>PHP Head Test</title>
    <?php $data; ?>
</head>

It's not actually doing anything, if you wanted to display it, you would use echo. This code does absolutely nothing except declare $data if it wasn't declared before-hand.

Generally, you should have most of your PHP code out of your HTML file, they should be in completely different files and the PHP code should include the HTML file. In this case, the PHP code that you put in the HTML file will have access to all PHP variables that were available in the file where it was included.

这篇关于在&lt; head&gt;中编写PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆