PDA

View Full Version : quick php syntax questions


mat
09-03-2002, 01:39 AM
hey,

just a couple of quick questions while i'm trying to learn the basics of php.

I have noticed in a tutorial that the authour put "@" symbols in fron of all the mysql functions i.e

$db = @mysql_connect

.. and

$query = @mysql_query


what are they there for?

Also I am slightly confused when i see "!" in front of a variable like so:

if (!$spanky) {

do this!

}

does it basically mean - if spanky doesn't exist then blah... ?


I know these are basic, just want to be clear :)

mat,

firepages
09-03-2002, 02:16 AM
Hi mat ,

@ before a function call supresses any error reports that the call may produce...

& you are spot on with the '!' which means NOT so...


<?
if(!@mysql_num_rows($yaks)){
//empty result so redirect//
header('location:try_again.php');
}else{
//we got some rows so do something//
//blah blah
}
?>



so in the above with error reporting set at normal levels , if the result($yaks) was empty, the mysql_num_rows() call would throw an error and cause the following header call to fail - the '@' takes care of that.

there are plenty who say its worng to use '@' because you should not ever be generating errors in the first place, I dont think that this is always realistic.

stuntboy
09-03-2002, 02:35 AM
lol I would like to see them guarantee the db server is not down when they try and connect to it. with the @ symbol you can send your own error message to the browser that will look much better lol

mat
09-03-2002, 03:48 AM
thanks for clearing that up.

mat,

freakysid
09-04-2002, 04:25 PM
The logic is slightly more complicated, and there is a trap you can fall into.

! is the NOT operator. It is a logic operator:

http://www.php.net/manual/en/language.operators.logical.php

It inverts the boolean value.

For example, a condition must be true for it's structure to be entred.

if ( this condition is true) {
then this code will be executed
}

Generally, in programming, anything that has a non-zero value will evaluate to TRUE and anything with a zero value will evaluate to FALSE.

The logical NOT operator "!" simply inverts the boolean value. Example

if (NOT(this condition is true)) {
then this code will be executed
}

So

!(true) == false;

and

!(false) == true;

The trap then is to use the NOT operator to test if a variable is set. PHP will declare a variable if it has not already been explicitly defined upon encountering it. By default, all variables are initialised to false.

So, generally, we could think that if a variable is not defined, and we test with this condition

if (!$spanky)

we can conclude that the variable $spanky has not been defined (in php speak - "not set").

However, what if $spanky was indead defined earlier in our script but holds a value that evaluates to false (for example zero). Sometimes one needs to make the subtle distinction between something that is defined but holds a value of false and something that is not defined.

In php the function isset() tests to see whether a variable has been defined regarless of what the value of that variable might evaluate to (true or false).

for example

$spanky = false;

if (! $spanky ) {
// this condition is met
// this code will be executed
}

if( ! isset($spanky) ) {
// this condition is not met as $spanky has been defined
// so this code will not be executed
}

:)

firepages
09-06-2002, 06:15 PM
The logic is slightly more complicated, and there is a trap you can fall into.


... etc , fair point I was not very clear there was I :)

I dont think in the example I gave that you need to use isset() , at least I cant see how (assuming it follows a query) it could give a misleading answer but thats in that example and certainly not in all cases so thats an inportant point.