Link to home
Create AccountLog in
Avatar of aej1973
aej1973

asked on

Need to understand these statements...

Can some one help me understand the following two statements;

if(!class_exists('abc_xyz'))
 {
 require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'something.php');
 }


------------------------------------------------------------------------------------------------

if(!isset($_SERVER['argv'][1]))
  {
    echo "Usage:\t{$_SERVER['_']} {$_SERVER['argv'][0]} peer\n\n";
    exit;
  }



Thanks for the help.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

The first statement checks to see if your script recognises a class called abc_xyz, and if it doesn't then it loads a file called something.php from the same folder as the script (which probably contains the code to declare the abc_xyz class).

The second statement checks to see if the script was passed an argument, such as a querystring. If it wasn't then it echoes out instructions on how to use the script, including the path to the PHP interpreter followed by the name of the script.
With respect to the first, you might want to check into this:
http://php.net/manual/en/function.autoload.php
Avatar of aej1973
aej1973

ASKER

Thank you for the explanation. Now, instead of

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'something.php');

cannot I just use: require_once('something.php'); ?
Avatar of aej1973

ASKER

Also, would it be possible for you to give me an example for the second statement ...just to help me understand it better. Thank you.
cannot I just use: require_once('something.php'); ?
Yes, I think that would work.

With respect to the second, please see this page.  It appears that it has something to do with running PHP from the command line.
http://php.net/manual/en/function.getopt.php

You might try var_dump($_SERVER) to see what is in those variables.
If you just require_once('something.php') it will look in the include path (as defined in php.ini) for the file, before looking in the script's current path. By specifying the full path as in the example above, it will only look in there (i.e it won't look in the include path - think of it as a safety net - only look where I want you to look!)

The second statement is basically a check to make sure you are calling the script correctly. Assume your script demands that you pass in an argument, such as:

myscript.php?name=chris

If you don't pass in the argument, then instead of running normally, it will display:

Usage: php.exe myscript.php peer

and then exit.

By the way, the \t is code for a TAB and the \n is code for a new line.
Avatar of aej1973

ASKER

ChrisStanyon, thank you for your explanation of the second script. Can you let me know what will be the values of : $_SERVER['argv'][0] and $_SERVER['argv'][1]. For some reason I am not able to get this code to work, I keep getting an output Usage: peer . For your ref I have attached the code I am working with, thanks again.

A
sip-show-peer.php
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of aej1973

ASKER

ChrisStanyon, thanks a ton, your explanation was so good and I have finally got my script to work...blessings!
Avatar of aej1973

ASKER

Thank you.