我有一些变量,我试图插入内部通信参数:
<?php
$user_id = "1234";
$intercom_name = "example";
$intercom_email = "example";
$intercom_name = "example";
$company_id = "example";
$company_name= "example";
?>
<script>
window.intercomSettings = {
app_id: "XYZ123",
id: "<?php echo $user_id; ?>",
name: "<?php echo $intercom_name; ?>",
email: "<?php echo $intercom_email; ?>",
company: {
id: "<?php echo $company_id; ?>",
name: "<?php echo $company_name; ?>",
}
};
</script>
然而,以这种方式使用php变量似乎打破了脚本,有人知道为什么吗?
答案 0 :(得分:0)
你的代码看起来不错但是你可以尝试使用json_encode()将javascript中的初始化对象导入php
<?php
$object=array(
"app_id"=>"XYZ123",
"id"=> "1234",
"name"=> "example",
"email"=> "example@example.com",
"company"=> array(
"id"=> "example",
"name"=> "example",
)
);
?>
<script>
window.intercomSettings = <?php echo json_encode($object); ?>;
</script>