[มือใหม่ php] มันใส่ค่าลงท้ายยังไงครับ

สมมุติว่า index.php?p=1

แล้วเขียนโค้ด

$_GET[“p”];

$content = file_get_contents(“http://www.web.com/content.php?adsID=จะเอาค่าpมาใส่ตรงนี้&room=paper”);

ทำไงจะเอาค่า p ไปใส่ตรงนั้นได้ครับ ผมเขียนเป็นแต่ asp เลยไม่รู้มันเขียน php ยังไง

$p = $_GET[“p”];
$content = file_get_contents(“http://www.web.com/content.php?adsID=$p&room=paper”);

แบบนี้เลยก็ได้ครับ

หรือ
$p = $_GET[“p”];
$content = file_get_contents(“http://www.web.com/content.php?adsID=".$p."&room=paper”);

หรือ
$p = $_GET[“p”];
$content = file_get_contents(‘http://www.web.com/content.php?adsID=’.$p.’&room=paper’);

เคื่องหมาย . เอาไว้สำหรับรวม string เข้าด้วยกันครับ เช่น
$str = “abcd” . “efgh”;
ผลลัพธ์ที่ได้ $str ก็จะเท่ากับ abcdefgh ครับ

เครื่องหมาย ’ (ขีดเดียว) กับ " (สองขีด) ใช้ไม่เหมือนกันนะครับ ถ้าตัวแปลอยู่ในเครื่องหมายแบบนี้ ‘$str hello world’ จะถือว่า $str เป็นแค่ string ไม่ใช่ตัวแปล
แต่ถ้าตัวแปลอยู่ในเครื่องหมายแบบนี้ “$str hello world” แบบนี้จะถือว่า $str เป็นตัวแปล ยกตัวอย่างเช่น

$str = ‘123456’;
$a = ‘$str hello world’;
echo $a; // ผลลัพธ์ที่ได้จะเป็น $str hello world
$b = “$str hello world”;
echo $b; // ผลลัพธ์ที่ได้จะเป็น 123456 hello world

สงสัยจะตอบยาวไป

พระเจ้าทรงโปรด ขอบคุณมากๆ :wub:

ค่าที่รับจาก url จะอยู่ในตัวแปล $_GET ครับ ส่วนค่าที่รับจากแบบฟรอ์มแบบ POST ก็จะอยู่ในตัวแปล $_POST
วิธีใช้ก็ง่ายๆ


index.php?p=1

$content = file_get_contents("http://www.web.com/content.php?adsID=$_GET['p']&room=paper");
หรือ
$content = file_get_contents("http://www.web.com/content.php?adsID=".$_GET['p']."&room=paper");

ส่วนการใช้
$p=$_GET[“p”];
ก็เป็นการถ่ายค่าตัวแปลไปยังอีกตัวแปลนึง เพื่อความสะดวกเวลาเรียกใช้งาน

สำหรับ code ตัวอย่างแนะนำให้เพิ่ม secure ดังนี้


$p=intval($_GET["p"]);
$content = file_get_contents("http://www.web.com/content.php?adsID=".$p."&room=paper");

[quote author=pin[KKE]yhost link=topic=18429.msg174184#msg174184 date=1238066823]


$p=intval($_GET["p"]);
$content = file_get_contents("http://www.web.com/content.php?adsID=".$p."&room=paper");

[/quote]

แบบนี้ก็ได้ inline casting


$content = file_get_contents("http://www.web.com/content.php?adsID=".(int)$p."&room=paper");

ขอบคุณมากครับ แฮ่ะๆ