[PHP] Single quoted strings (was: ereg_replace to preg_replace translation)

2009-08-11 Thread Martin Scotta
On Tue, Aug 11, 2009 at 12:21 PM, Ford, Mike m.f...@leedsmet.ac.uk wrote:

  -Original Message-
  From: m a r k u s [mailto:queribus2...@hotmail.com]
  Sent: 11 August 2009 15:34
 
  I see that from PHP 5.3.0 ereg_replace() function is deprecated and
  throws a warning.
  I would like to use the preg_replace() function as an alternative of
  ereg_replace() function but...
  can't undestand the \n#[^\n]*\n expression.
 
  $sql = ereg_replace(\n#[^\n]*\n, , $sql);

 Generally the only change you need to make for transition from ereg to preg
 (for simple expressions, anyway) is the addition of pattern delimiters. So
 the above becomes, for example:

  $sql = preg_replace(|\n#[^\n]*\n|, , $sql);

 Although I would argue that those \ characters should be escaped (and
 should have been even for ereg), so the more correct version of this is:

  $sql = preg_replace(|\\n#[^\\n]*\\n|, , $sql);


 Cheers!

 Mike
  --
 Mike Ford,
 Electronic Information Developer, Libraries and Learning Innovation,
 Leeds Metropolitan University, C507, Civic Quarter Campus,
 Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
 Email: m.f...@leedsmet.ac.uk
 Tel: +44 113 812 4730





 To view the terms under which this email is distributed, please go to
 http://disclaimer.leedsmet.ac.uk/email.htm

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


You can use a single quote string (instead of escape the \n)
The result is the same, but is more legible.

$sql = preg_replace('|\n#[^\n]*\n|', '', $sql);

Personally I try to not use double quoted.
PHP parses single quoted very much faster.

# for this
echo Hi, $name, wellcome $home;

# I use
echo 'Hi, ', $name, ', wellcome ', $home;

# or
printf( 'Hi, %s, wellcome %s', $name, $home );


And of course, this is a matter of taste!

-- 
Martin Scotta


Re: [PHP] Single quoted strings (was: ereg_replace to preg_replace translation)

2009-08-11 Thread Ben Dunlap

 Personally I try to not use double quoted.
 PHP parses single quoted very much faster.

 # for this
 echo Hi, $name, wellcome $home;

 # I use
 echo 'Hi, ', $name, ', wellcome ', $home;


I'm not sure if this was true in older versions of PHP, but it's not so much
any more, and I wonder if it was ever worth the loss of readability.

Interesting discussion about this on the Google Group Make the Web Faster.
The focal points of the discussion are an article by a lead Google engineer,
and then a point-by-point refutation by a PHP core developer. Here's the
refutation:

http://groups.google.com/group/make-the-web-faster/browse_thread/thread/ddfbe82dd80408cc

Ben