Home  |  Linux  | Mysql  | PHP  | XML
From:Nathan Wallace Date:Fri Aug 13 00:08:57 1999
Subject:PHP Knowledge Base Update -- August 12th, 1999
PHP has just been awarded runner up at Linuxword in the Programming
Library and Tools Category:

    http://www.linuxworld.com/linuxworld/lw-1999-08/lw-08-penguin_1.html 

There was a fairly long discussion on the list today regarding the
adding of ICQ functions or a class to PHP:

<http://www.progressive-comp.com/Lists/?l=php3-general&m=93443639623904>

Cheers,

Nathan


------------------------------------------------------------
How can I generate a random alphanumeric password?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/590
------------------------------------------------------------
Brian Schaffner

Try this:

    $myPass = substr(ereg_replace("[^A-Za-z]", "", crypt(time())) .
                         ereg_replace("[^A-Za-z]", "", crypt(time())) .
                         ereg_replace("[^A-Za-z]", "", crypt(time())),
                     0, 6);

the three crypts should ensure that you get enough Alpha characters.
crypt() by itself returns 13 "printable ASCII characters", the first 2
being the salt which is in the set [a-zA-Z0-9./]


------------------------------------------------------------
How can I generate a random number in PHP?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/591
------------------------------------------------------------
David den Boer, Sander Pilon, Teodor Cimpoesu

The examples below produce a random number between 0 and 5 inclusive. 
That is, the possible values are 0,1,2,3,4,5.

This works for me every time :

      srand((double)microtime()*1000000);
      $randval = rand(0, 5);

If you have an old version of PHP then you might need to use this
instead:

    srand((double)microtime()*10000);
    $t=rand();
    $number = rand()%6;

Alternatively, for a very simple random generator use:

    $number = time() % 6;


------------------------------------------------------------
How can I convert an array into a string?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/592
------------------------------------------------------------
Werner Stuerenburg

Try using the implode function:

    http://www.php.net/manual/function.implode.php3

Producing a string:

    $colon_separated_string = implode($array, ":");

printing out the array:

    echo implode($output, " ");


------------------------------------------------------------
How can I split a long string into lines without breaking up words?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/593
------------------------------------------------------------
Brian Schaffner

Try this function:

<?
    function SplitIntoLines ($str, $n, $insert, $max=0, $conserve=False,
$break=" ") {
        static $count = 0;
        $count++;
        if ($max > 0) {
            if ($count >= $max) {
                return $str;
            }
        }
        $retval = False;
        if (strlen($str) <= $n) {
            $retval = $str;
        } else {
            if (!$conserve) {
                $begin = substr($str, 0, $n);
                $end = substr($str, $n, strlen($str) - $n);
                $retval = $begin . $insert . SplitIntoLines($end, $n,
$insert, $max);
            } else {
                $begin = substr($str, 0, $n);
                $pos = strrpos($begin, $break);
                if ($pos >= 1) {
                    $begin = substr($str, 0, $pos);
                    $end = substr($str, $pos+1, strlen($str) - $pos +
1);
                    $retval = $begin . $insert . SplitIntoLines($end,
$n, $insert, $max, $conserve, $break);
                }
            }
        }
        return $retval;
    }
    $foo = "One two three four five six seven eight nine ten.";
    $x = SplitIntoLines($foo, 38, "\n", 0, True, " ");
    echo $x;
?>

In this case, $str is the string, $n is the max number of characters per
line, $insert is what to put in between each line, $max is the maximum
number of lines to return, $conserve will only break lines at the $break
character.


** OTHER RANDOMLY SELECTED PHPKB ENTRIES **


------------------------------------------------------------
Can I stop a page being displayed until the whole script has been
parsed?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/52
------------------------------------------------------------

Not easily.  PHP4 will have support for output buffering.

In the meantime, you can accumulate your data in an array, and then
output the array conditionally. Replace your "echo" or "print" commands
with:

 $line[] = 

and then when you figure out you need to redirect, just redirect; else
loop over $line and echo every row.

It's a low-tech solution, but it works.


------------------------------------------------------------
Does PHP support COM or DCOM?
Does PHP support JAVA?
Does PHP support CORBA?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/289
------------------------------------------------------------

PHP 3.0 supports a very limited aspect of COM, in a very ugly notation 
(it's probably not practically-useable).  PHP 4.0, on the other hand,
takes advantage of Zend's new OO syntax overloading to implement a 
full-featured COM module, that allows easy OO access to COM (or DCOM) 
objects that are scriptable.

We'll be looking into Java and CORBA support in the future.


------------------------------------------------------------
Can I return a value from a class constructor?
How can I return an error from a class constructor?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/419
------------------------------------------------------------

The class constructor is executed when creating an instance of the
class.  It always returns the new class instance.

If you need to return something from the constructor then you can work
around this.  For every PHP class you create, have a variable called
$retval.  Then, set $retval inside the constructor to whatever you would
want to return. Then, after calling $x = new foo() - check the value of
$x->retval.

One case where you may like to return a different value from a
constructor is if an error occurs while creating the object.  There is
one way to do what you want to do (I assume that you want to check
whether the instance was initialized correctly):

--- BEGIN PSEUDOCODE ---
class <classname>
{
 function <classname>(<init args>,&$error)
 {
  if(<init goes ok>) $error=false;
  else $error=true;
 }
}

$instance= new <classname>(<init args>,$error);
if($error==true) <handle the error>
else <use the object>
--- END PSEUDOCODE ---

  Thus, when you call the constructor of the class you pass a reference
to an error checking variable that will contain (in the above case) true
or false, depending on whether the instance was initialized correctly or
not. Of course, you can make more elaborate checking mechanisms, but the
above shows the principle.
  Just to illustrate, here is a working example that you can use as
a basis for further development:

--- BEGIN PHP CODE ---
<?PHP
class FortyTwo
{
 var $value;
 var $isValid=false;

 function FortyTwo($number,&$error)
 {
  if($number==42) {
   $this->isValid=true;
   $this->value=$number;
   $error=false;
  } else $error=true;
 }

 function getValue()
 {
  if($this->isValid) return($this->value);
  else return(0);
 }
}

// This test fails
$fortyTwo=new FortyTwo(41,$error);
if($error) printf("There was an error while initializing the
object.<BR>");
else printf("The object was initialized correctly, the value is %d<BR>",
 $fortyTwo->getValue());

// This test succeeds
$fortyTwo=new FortyTwo(42,$error);
if($error) printf("There was an error while initializing the
object.<BR>");
else printf("The object was initialized correctly, the value is %d<BR>",
 $fortyTwo->getValue());
?>
--- END PHP CODE ---

  Of course, the method of using object variables for the error codes
and messages will suffice, but I think the above method is what you are
after (as you do not need to have extra variables inside the object that
you have to check). I think this can be used in subclasses as well to
check whether the parent class was initialized... but I digress.
  (Actually, I am using this somewhere the other way around - I am
calling
a function that may or may not return an object. Thus -

--- BEGIN PSEUDOCODE ---
function getSomeObject(<arguments>,&$objRef)
{
 if(<object is ok>) return(true);
 else {
  $objRef=false;
  return($objRef);
 }
}

if($retcode=getSomeObject(<arguments>,&$objRef)) <use the object>
else <handle the error>
--- END PSEUDOCODE ---

...well, that is a very simple example but probably illustrates what I
am doing with the variable reference.)


------------------------------------------------------------
Can I execute Oracle stored procedures in PHP?
Do I have access to Oracle OUT parameters in PHP?
How do I access Oracle stored procedure return values in PHP?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/445
------------------------------------------------------------

For binding to an stored procedure OUT variable see the documentation 
for Oracle 8 (OCI) -> OCIBindByName. Perfeclty simple.

Return values: I use an extra OUT variable for each stored proc. called
errnum. That is my return value. I bet you could do functions with
return values in OCI but I havent tried it yet. The other retun value
(that you probably don't mean) is the return value of the OCI Execute
stmt. That just says if your PL/SQL query worked or if there was some
ora-xxxx error.


------------------------------------------------------------
What is the client server sequence for setting a cookie?
How can I check if a browser accepts cookies?
http://e-gineer.com/e-gineer/phpkb/view.phtml/qid/400
------------------------------------------------------------

Here is the cookie setting sequence in PHP:

    Client                      Server

    requests page
                                gets request
                                sets cookie in reply
                                sends reply
    gets reply (with cookie)
    sets cookie on machine

    sends new request (with
        cookie included)
                                gets request
                                finds cookie in request
                                sets variable in script
                                runs script

The server has to interact with the client to be able to set a cookie or
determine if it has been set.

From http://www.php3.com/manual/function.setcookie.php3

    <?php

    $status = 0;
    if (isset($myTstCky) && ($myTstCky == "ChocChip")) $status = 1;
    if (!isset($CCHK)) {
        setcookie("myTstCky", "ChocChip");
        header("Location: $PHP_SELF?CCHK=1");
        exit;
    }
    ?>

    <html>

    <head><title>Cookie Check</title></head>
    <body bgcolor="#FFFFFF" text="#000000">
    Cookie Check Status:

    <?php
        printf ('<font color="#%s">%s</font><br>;',
            $status ? "00FF00" : "FF0000",
            $status ? "PASSED!" : "FAILED!");
    ?>

    </body>
    </html>



Navigate in group php.kb at sever news.php.net
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by Zareef Ahmed
Powered By PHP Consultants