quakenet:#php FAQ
| | 1.
Which is the best x? |
|---|
http://faq.php-quake.net/#best |
A common question is 'Which is the best text editor,
operating system, webserver, database ...?'.
The result of such a discussion about which is the best is mostly
that no party changes their opinion.
The problem is that no 'best' exists in hardly any case.
There is always a pro and contra for and against everything.
A few classic examples are:
- vim vs. emacs
- open source vs. closed source
- windows vs. *nix
- unix derivative vs. unix derivative
I think it depends first of all always on the context what is
closest to the 'best', and additionally it is definitely a matter
of taste, as you just see with McDonalds vs. Burger King or
Pepsi vs Coke :).
-- matled |
| 2.
PHP reports "Notice: Use of undefined constant x - assumed 'x' [...]" |
|---|
http://faq.php-quake.net/#constant | |
PHP reports this error, if a string isn't quoted.
This can happen when you use functions or try to access an array
index.
<?php a_function(test); // wrong $array[key] = 'foo'; // wrong ?>
PHP interprets test and key as constants
which do not exist, but corrects this mistake and throws out a
notice.
The array index key or the parameter test
has to be within singlequotes (') or doublequotes (").
But if the value is no string
(e.g. an integer) or a numeric
array index, then it will not be quoted.
Below are some examples to show what is correct and what is wrong.
<?php a_function(test); // wrong a_function('test'); // correct, since it is in Singlequotes a_function("test"); // correct, since it is in Doublequotes a_function(123); // correct, because an integer is passed a_function('123'); // correct, too, but here it is a string with the value '123' $array[key] = 'foo'; // wrong $array['key'] = 'foo'; // correct $array["key"] = 'foo'; // correct $array[0] = 'foo'; // correct, since it is a numeric index ?>
-- gix |
| 3.
I'm looking for a good PHP-editor |
|---|
http://faq.php-quake.net/#editor | |
There are a lot of PHP-editors on the market.
Some are expensive, some are cheap and some are free.
Here is a list of some PHP-editors and editors with PHP
syntax-highlighting:
Which editor you use is your choice (see http://faq.php-quake.net/#best).
-- Progman |
| 4.
My script does not work as I want it to but PHP does not report any errors. |
|---|
http://faq.php-quake.net/#error | |
A frequent problem consists of error occurances and error messages
being suppressed by the PHP configuration.
Consequently the script does not work completely as you want it to
but you even have no hint where the problem could be.
To enable the output of the error messages, add this two lines of
code at the top of your script.
<?php ini_set('display_errors', 1); error_reporting(E_ALL); ?>
The function error_reporting() allows to adjust which error messages
should be displayed.
For further information read
http://php3.de/error_reporting.
-- matled |
| 5.
Is it in PHP >= 4.2 impossible to use variables in the url? |
|---|
http://faq.php-quake.net/#globals | |
Sure it is still possible; but from PHP 4.2 upwards
register_globals is by default set to Off.
register_globals On makes parameters given to the
script by GET, POST and cookie available as variables (e.g. if
you call ?foo=bar, a variable $foo
with the value 'bar' exists).
This can be a security hole if you use uninitialized variables.
By that the visitor is enbled to set these variables, and if the
script then uses these variables as 'trusted' variables even if they
do not deserve it.
If register_globals is set to Off, you can
access the GET, POST and cookie variables by the arrays $_GET,
$_POST, $_COOKIE, and alternatively by
$HTTP_*_VARS (* being GET, POST or COOKIE).
The difference between the $_* and
$HTTP_*_VARS variables is that $_*
is superglobal; the result is that you do not have to do a
global $_GET; before using $_GET in
functions.
More detailed information is available in the manual:
http://php3.de/manual/en/security.registerglobals.php.
-- matled |
| 7.
I get "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource [...]" |
|---|
http://faq.php-quake.net/#mysql | |
<?php $result = mysql_query('SELECT * FROM Table WHERE Field=Text'); while($row = mysql_fetch_array($result)) { /* do some stuff */ } ?>
PHP complains because $result is not a valid MySQL result resource -
why could this happen?
Well, usually because your SQL query failed, this can have several
reasons, for example typos or missing quotes.
You have to let your error handling know which error MySQL produced.
For Example:
<?php $query = "SELECT * FROM Table WHERE Field='Text'"; $result = mysql_query($query); if (empty($result)) { trigger_error('MySQL ERROR ('.mysql_errno().'): "'. mysql_error().'", the query was: "' . $query . '".', E_USER_ERROR); } while($row = mysql_fetch_array($result)) { /* do some stuff */ } ?>
In case mysql_query() failed, the script will
stop and display the error message including the line in which the
error happened.
You can also use this error handling for
mysql_connect() and mysql_select_db().
After you debugged your script successfully, you might want to replace
the trigger_error() by a block of code that handles the
error in a sane way (logging, etc).
For advanced database access you should have a look at PEAR::DB.
-- sica |
| 8.
How do I redirect to another site/page? |
|---|
http://faq.php-quake.net/#redirect |
There are three ways to redirect the user to an other site/page. One with the HTML-meta-tag,
one with javascript and one with header-handling.
In HTML there is a way to redirect the user to an other site/page with a meta-tag,
but this way isn't accepted by the W3C.
The meta-tag with its attributes looks like this:
<meta http-equiv="refresh" content="sec; URL=new_url">
In place of sec just enter the number of seconds you want to delay
the redirection to the new url. In place of new_url put the
URL to redirect to. Both relative and absolute URLs are possible.
Of course, the meta-tag must be inside the head-tag. This redirect is also called
meta-refresh.
<meta http-equiv="refresh" content="0; URL=http://faq.php-quake.net/#redirect">
<meta http-equiv="refresh" content="5; URL=topic.php?id=5">
The second way to redirect is to use javascript. You only have to
change the javascript-variable window.location.href
like this:
window.location.href = "http://www.mein-server.de/";
// or
window.location.href = "index.html";
You are allowed to use javascript at all places, but not every browser is able to
execute javascript.
The third way to redirect can be used with PHP. You can edit the HTTP header values,
which are sent to the browser at every request, with the header()
function. One of these header values is the Location value, which we
edit in this situation to redirect the user to another page.
<?php header("Location: absolute_URL"); ?>
The value for the Location attribute must be an absolute URL, started with http://
or similar. Here are some examples.
<?php header("Location: http://www.my-server.de/index.php"); header("Location: ftp://ftp.foo.com/file.zip"); header("Location: http://www.my-server.de/forum.php?forum=2"); ?>
Of course the header values must be sent before any outputs. This means, you cannot use
functions like echo, printf and html-tags before header(). A single space before the
<?php
is too much. If you get a warning message like "Cannot modify header information -
headers already sent by..." check that you
do not have any output before header(). In the error message itself is the information
where the output starts ("output started at File: Line"). This restricts the
header function extremly, but every browser is able to understand this kind of forwarding
(as it is part of the HTTP protocol).
-- Progman |
| 9.
What does 'Resource ID #x' mean? |
|---|
http://faq.php-quake.net/#resource | |
Resource IDs are returned by a few functions (mysql_query, fopen, e.t.c.).
These are merely 'references' to the resource. To get the information you
want, you need to use another function (mysql_fetch_array, fgets, e.t.c.),
and pass the resource ID as an argument.
e.g:
<?php /* Undesired */ $file = fopen("myfile.txt","r"); echo $file; // Will echo "Resource id #x", which is probably not what you wanted. var_dump($file); // This echo the debuginfo "resource(#) of type (stream)"
$result = mysql_query("SELECT * FROM `foo`"); echo $result; // Will echo "Resource id #x", which is probably not what you wanted. var_dump($result); // This echo the debuginfo "resource(#) of type (mysql result)"
/* Desired */
$file = fopen("myfile.txt","r"); $line = fgets($file); echo $line; // Will echo the first line of the file, which is probably the desired result.
$result = mysql_query("SELECT * FROM `foo`"); $array = mysql_fetch_array($result); echo $array['bar']; // Will echo the value of column 'bar' of the first row in table 'foo' ?>
-- MD87 |
| 10.
What does 'Notice: Undefined variable / index: <variable or array element name> ...' mean? |
|---|
http://faq.php-quake.net/#undefined |
Since PHP 4.3.4 the error_reporting level has been increased to E_ALL. At this level, PHP will display an error when an undefined variable or array element is referred to, except in the case of isset(). To fix this, you need to either define a value for the variable or array element before it is used, or use isset() to check that it exists.
Instead of:
<?php $action = $_POST['action']; ?>
Use:
<?php if (isset ($_POST['action'])) { $action = $_POST['action']; } else { $action = ''; } ?>
Instead of:
<?php // Error checking code // If an error occurs $error_count is increased if ($error_count != 0) { echo "There was a problem!"; } ?>
Use:
<?php // _always_ initialize variables before using them $error_count = 0; // Error checking code // If an error occurs $error_count is increased if ($error_count != 0) { echo "There was a problem!"; } ?>
You should not use the @ symbol to silence errors. For example it has been suggested by some that to solve the error appearing in the following code:
<?php echo $error; ?>
The coder should change it to:
<?php @echo $error; ?>
Although this does make the error disappear from your screen, this is all it does. The problem still exists. Also, any other errors that may occur on this line are also silenced, so you could be left with much more serious errors in your script.
You should also not decrease the error_reporting level in your php.ini to less than E_ALL. In this case the error will not be shown on your server, but it will be shown on other servers.
You should not use ini_set() to change the error_reporting level in just your script, this is because some webspace providers can and do disable the ini_set() command. An error_reporting level as high as possible also encourages better coding style that will allow your scripts to operate better on a wider range of servers.
Note: One frequent reason for recieving this error is following outdated tutorials. Since PHP 4.2.x the setting php.ini register_globals defaults to off. You can read more about this at http://php.net/register_globals.
-- Athon Solo |
| 11.
Which editor do you use? |
|---|
http://faq.php-quake.net/#which |
We are often asked, which editors we are using. Now there is a list
which will be updated from time to time:
-
bluefish - LittleIdiot
-
HTML-Editor (Phase 5) - aoh-hell
-
kwrite - Progman
-
quanta - LittleIdiot
-
UltraEdit - matled, gix
-
(g)vim - bigfoot, ch, et, gix, LittleIdiot, matled, Progman, soohrt, sica, Thomas
-
Weaverslave - Thomas
See also http://faq.php-quake.net/#best and
http://faq.php-quake.net/#editor.
-- Progman |
|