Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->Linux程序设计 ->正文

Linux编程:把PHP作为Shell脚本使用

来源: 作者: 时间:2007-04-11 点击: [收藏] [投稿]









 

三、在其他语言编写的Shell脚本中包含PHP编写的Shell脚本:

有时候我们可能需要在其他语言编写的Shell脚本中包含PHP编写的Shell脚本。其实非常简单,下面是一个简单的例子:

 echo This is the Bash section of the code. 
  
  /usr/local/bin/php -q << EOF <?php 
  print("This is the PHP section of the coden"); 
  ?> 
  EOF

其实就是调用PHP来解析下面的代码,然后输出;那么,再试试下面的代码:

echo This is the Bash section of the code. 
  
  /usr/local/bin/php -q << EOF <?php 
  $myVar = 'PHP'; 
  print("This is the $myVar section of the coden"); 
  ?> 
  EOF

可以看出两次的代码唯一的不同就是第二次使用了一个变量“$myVar”,试试运行,PHP竟然给出出错的信息:“Parse error: parse error in - on line 2”!这是因为Bash中的变量也是“$myVar”,而Bash解析器先把变量给替换掉了,要想解决这个问题,你需要在每个PHP的变量前面加上“\”转义符,那么刚才的代码修改如下:

echo This is the Bash section of the code. 
  
  /usr/local/bin/php -q << EOF <?php 
  \$myVar = 'PHP'; 
  print("This is the \$myVar section of the coden"); 
  ?> 
  EOF

()

  

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:Linux脚本开发数学库在PHP中的重要性   下一篇:Linux编程之序列化存储Python对象(上)

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号