Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ตัวดำเนินการรันคำสั่ง (Execution Operator) ใน PHP

บทนำ

มีตัวดำเนินการรันคำสั่ง (Execution operator) หนึ่งตัวใน PHP สตริงภายใน back-tick (``) จะถูกจัดการเป็นคำสั่ง DOS (คำสั่ง shell ใน UNIX/Linux) และผลลัพธ์จะถูกคืนค่า ตัวดำเนินการนี้ทำงานคล้ายกับฟังก์ชัน shell_exec() ใน PHP

โค้ดต่อไปนี้รันคำสั่ง DIR และคืนผลลัพธ์เป็นสตริง:

ตัวอย่าง

<?php $list=`dir *.php`; echo "$list"; ?>

ผลลัพธ์

Volume in drive C is Windows 10 Volume Serial Number is 540D-CE99 Directory of C:\xampp\php 01/27/2016 05:32 PM 18,869 CompatInfo.php 07/08/2020 06:40 PM 64 test.php 07/11/2020 02:13 PM 48 testscript.php 03/30/2013 05:59 PM 1,447 webdriver-test-example.php 4 File(s) 20,428 bytes 0 Dir(s) 178,002,157,568 bytes free

นี่คือตัวอย่างอีกอย่างของ backtick operator รันคำสั่ง type:

ตัวอย่าง

<?php $list='type testscript.php'; echo "$list"; ?>

ผลลัพธ์

type testscript.php