温馨提示×

PHP命令行脚本如何输出结果

PHP
小樊
148
2024-11-06 00:09:31
栏目: 编程语言

在PHP中,你可以通过以下几种方式在命令行脚本中输出结果:

  1. 使用echo语句:
<?php echo "Hello, World!"; ?> 
  1. 使用print语句:
<?php print "Hello, World!"; ?> 
  1. 使用printf语句:
<?php printf("Hello, %s!", "World"); ?> 
  1. 使用var_dump()函数:
<?php $variable = array("foo" => "bar", "baz" => "qux"); var_dump($variable); ?> 
  1. 使用print_r()函数:
<?php $variable = array("foo" => "bar", "baz" => "qux"); print_r($variable); ?> 
  1. 使用shell_exec()函数执行系统命令并输出结果:
<?php $output = shell_exec('ls'); echo "<pre>$output</pre>"; ?> 
  1. 使用file_get_contents()函数读取文件内容并输出结果:
<?php $file_content = file_get_contents('example.txt'); echo "<pre>$file_content</pre>"; ?> 

这些方法都可以让你在PHP命令行脚本中输出结果。根据你的需求,可以选择合适的方法来显示信息。

0