p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: really ugly software


Message #1 by "Lawrence" <spam@k...> on Sat, 5 Oct 2002 00:49:58 -0400
The following functions works, but damn, what a bunch of hacks. In particular,
amazzzzihaigooohanMack1234. It's a
crime, I know. None of the other tests seem to work right, not empty() nor if (!$var) nor if ($var). I kept getting
unexpected results for the 0 that one my functions put into the form automatically. stristr() was the only test I
could find in the end, though I'm wondering if there was some other way to simply test for the presense of a case
insensitive string. I saw about 60 functions on www.php.net, but this was the only one that seemed to work.

Next I'd like to add locking to this function. In semi-psuedo code, I think locking works something like this, yes?

flock($fp, LOCK_EX);
 $fp = fopen("parsedLinesPrintedToPage.htm", "w") or die("Wasn't able to open the file on the web host.");
  fwrite ($fp, $fileString) or die("Had trouble writing all the information to disk.");
 fclose($fp);
flock($fp, LOCK_UN );






function parseLinesFromDocument() {
 global $PHP_SELF, $firstKeyword, $firstKeywordExtraLine, $secondKeyword, $secondKeywordExtraLine, $thirdKeyword,
$thirdKeywordExtraLine;
 $self = $PHP_SELF;
 $fcontents = file("collateWebPagesProject.htm");

 if ($firstKeyword == "") $firstKeyword = "amazzzzihaigooohanMack1234";
 if ($secondKeyword == "") $secondKeyword = "amazzzzihaigooohanMack1234";
 if ($thirdKeyword == "") $thirdKeyword = "amazzzzihaigooohanMack1234";
 if ($firstKeyword == "0") $firstKeyword = "amazzzzihaigooohanMack1234";
 if ($secondKeyword == "0") $secondKeyword = "amazzzzihaigooohanMack1234";
 if ($thirdKeyword == "0") $thirdKeyword = "amazzzzihaigooohanMack1234";

 $allLinesReturned = array();
 for ($i=0; $i < count($fcontents); $i++) {
  $r = $i + 1;
  if (stristr($fcontents[$i], $firstKeyword)) {
   if ($firstKeywordExtraLine) {
    $allLinesReturned[] = "<br><br>".$fcontents[$i];
    $allLinesReturned[] = $fcontents[$r];
   } else {
    $allLinesReturned[] = $fcontents[$i];
   }
  } else {
   if (stristr($fcontents[$i], $secondKeyword)) {
    if ($secondKeywordExtraLine) {
     $allLinesReturned[] = "<br><br>".$fcontents[$i];
     $allLinesReturned[] = $fcontents[$r];
    } else {
     $allLinesReturned[] = $fcontents[$i];
    }
   } else {
    if (stristr($fcontents[$i], $thirdKeyword)) {
     if ($thirdKeywordExtraLine) {
      $allLinesReturned[] = "<br><br>".$fcontents[$i];
      $allLinesReturned[] = $fcontents[$r];
     } else {
      $allLinesReturned[] = $fcontents[$i];
     }
    }
   }
  }
 }
 $fileString = implode("", $allLinesReturned);

 $fp = fopen("parsedLinesPrintedToPage.htm", "w") or die("Wasn't able to open the file on the web host.");
 if ($fileString != "") {
  fwrite ($fp, $fileString) or die("Had trouble writing all the information to disk.");
  echo "The file, which contains all those lines that had at least one of the keywords which you specified, <a
href='parsedLinesPrintedToPage.htm'>has been created and can be viewed here</a>. <br><br>You can also <a
href='$self'>go back to the main page to begin working with this document.</a>";
 } else {
  echo "There were no matches. None of your keywords were in the document.";
 }
 fclose($fp);
}





Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 7 Oct 2002 11:01:23 -0700
To check if a string is non empty, I usually use

if(isset($var) && ($var != ''))
{
   ...
}


That seems to work well.  Because of lazy evaluation, if $var is not set, isset
will return false and prevent the ($var != '') from executing, thus preventing
a NOTICE level warning about using an unitialized variable.

In your case, you're checking to see if a string _IS_ empty, which is the
negation of the above.

!(A && B)  is equivalent to (!A || !B).  (That's DeMorgan's law)

if(!isset($var) || !($var != ''))
{
   ...
}


However we see that the second expression can be written simply as
  ($var == '')


to sum up:

if(!isset($var) || ($var == ''))
{
   ...
}


> Next I'd like to add locking to this function. In semi-psuedo code,
> I think locking works something like this, yes?
>
> flock($fp, LOCK_EX);
> $fp = fopen("parsedLinesPrintedToPage.htm", "w")
>     or die("Wasn't able to open the file on the web host.");
> fwrite ($fp, $fileString)
      or die("Had trouble writing all the information to disk.");
> fclose($fp);
> flock($fp, LOCK_UN );

Close, but not quite -- the calls to flock() require a valid file pointer.  You
need to call them between the calls to fopen() and fclose().

if($fp = fopen(...))
{
   if(flock($fp, LOCK_EX))
   {
      fwrite($fp, ...);
      $flock($fp, LOCK_UN);
   }
   fclose($fp);
}


take care.

nik


  Return to Index