Can anyone explain what the $PHP_SELF global variable does in the following script?

<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
do {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"],
$myrow["first"], $myrow["last"]);
} while ($myrow = mysql_fetch_array($result));
} else {
echo "Sorry, no records were found!";
}
?>
</body>
</html>

PHP_SELF is a variable that returns the current script being executed. This variable returns the name and path of the current file (from the root folder).

Let's say the code you posted above is on a file named 'test.php' and that the URL to that file (which you are currently executing) is http://yoursite.com/somefolder/test.php $PHP_SELF then equals "/somefolder/test.php".

In your printf() function you have: <a href="%s"...> that first %s is replaced with the value of $PHP_SELF

php help is for helping code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.