Re: [PHP] Invoking functions stored in a separate directory?

2009-08-22 Thread Clancy
On Fri, 21 Aug 2009 15:16:11 +0200, ak...@telkomsa.net ("Arno Kuhl") wrote:

>-Original Message-
>From: Clancy [mailto:clanc...@cybec.com.au] 
>Sent: 21 August 2009 01:26 PM
>To: php-general@lists.php.net
>Subject: [PHP] Invoking functions stored in a separate directory?
>
>I am developing an idea for a website engine which can be shared between
>several different websites. Each website would have its own directory under
>a common root directory, and the engine would be in a separate directory
>Engine:
>
>Root
>Website_1.com, Website_2.com, Engine
>
>The website directories would each contain the design data for that website,
>consisting basically of a mixture of text files and images. The various
>pages would be loaded by loading index.php from the website root directory,
>and specifying a number of parameters e.g.
>
>http://www.corybas.com/index.php?path=Holidays&level=0&item=0
>
>I have the minimum amount of code in index.php -- just enough to set some
>parameters to identify the website, and then include
>../Engine/Main_prog.php.  This in turn can include any of a large number of
>other include files to carry out particular functions. 
.
>
>Using include ../Engine/Main_prog.php won't work for you in a production
>environment. You need to create a path.php file that defines the absolute
>path to the engine for each website, and include it at the top of your
>website script. Then you can do something like:
>
>   include ENGINEPATH."Main_prog.php";

Thank you very much for this. It has at last provided the clue I was looking 
for, and
after far too long I have got a trivially simple demonstration working. One 
complication I
hadn't anticipated is that apparently I can only access functions in the 
include file, but
cannot execute any code in it. 

Thus when Halla.php read:

Eng_test_'.__LINE__.' Hallejuha!! : ';  ?>

and I included it:

$ok = define ('HOST_PATH','../Engine');
$ok = include (HOST_PATH.'/Halla.php');

nothing happened.

When Halla.php read:

Eng_test_'.__LINE__.' Hallejuha!! : '.$i.''; 
return $i; } ?>

and I included it:

$ok = define ('HOST_PATH','../Engine');
$ok = include (HOST_PATH.'/Halla.php');
$ok = halla (7);
echo ' Returned '.$ok'';

it did work.  I presume that this explains your next comment, in that I am 
neither reading
nor executing the code in the Include, so permissions don't come into it.

>You shouldn't really have permission problems as long as your website and
>engine are on the same server.  ...

>One other advantage it will give you for your particualr design is that you
>can have multiple engines per server (e.g. Engine1, Engine2, etc) so that
>you can bring one engine down for upgrade while still keeping sites running
>on the other engines.

Yes; I have got already got all that working.  Now I JUST have to convert my 
main program
into one big function!

Will I still be able to access $_GET & $_POST variables, and set up session 
variables from
inside this function? I guess I can declare them all GLOBAL?

Thanks, again,

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



Re: [PHP] daemon without pcntl_fork

2009-08-22 Thread Lars Torben Wilson
2009/8/20 Jim Lucas :
> Lars Torben Wilson wrote:
>> 2009/8/19 Per Jessen :
>>> Jim Lucas wrote:
>>
>> [snip]
>>
>>> I probably wouldn't have chosen PHP for the first one, but there's no
>>> reason it shouldn't work.  For the second one, did you mean to
>>> write "serial port"?  That's a bit of a different animal, I'm not sure
>>> how far you'll get with php.
>>>
 Here is what I have come up with so far.  Looks to satisfying my
 needs:

 tms_daemon
>>> [snip]
 # END OF SCRIPT
>>> Looks good to me. It'll certainly do the job.
>>>
>>>
>>> /Per
>>
>> I agree with Per on all points--I probably wouldn't choose PHP as a
>> first choice for the first task, but the startup script you have shown
>> looks like a bog-standard startup script and should serve you well. I
>> haven't really gone over it with a fine-toothed comb though. Typos
>> etc. are still up to to you. :) Of course, the whole thing depends on
>> how tms_daemon behaves, but the startup script looks OK.
>>
>> Can you explain in a bit more detail exactly what the second part (the
>> serial-network data logger) needs to do? I've written similar daemons
>> in C but not PHP--when I've needed to get something like that going
>> with a PHP script, I've used ser2net (which I linked to in an earlier
>> post) and been quite happy. Maybe you don't even have to do the hard
>> work yourself (or buy extra hardware to do it for you).
>>
>>
>> Cheers,
>>
>> Torben
>>
>
> As for the second project, I asked about it in the previous thread about
> the SMDR/CDR processor.
>
> http://www.nabble.com/SMDR-CDR-daemon-processor-td25014822.html

Sorry, missed that thread.

> Basically, I need to have a process that collects data via serial
> (USB|RS323), connects to a remote port, or listens and receives data
> from the PBX pushing it.
>
> The most common is the RS232 serial connection.  Thats why I need to be
> able to listen on the local RS232 port for data coming from the PBX system.
>
> What I have built so far can connect to a remote machine, via a TCP/IP
> connection, and wait for data to be push out the specified TCP port.
>
> I haven't done it yet, but I know that I can easily build, using a
> different project as a base, the version that would connect to a local
> TCP/IP IP:PORT and wait for a PBX to send the data to that IP:PORT.
>
> After the data has been received, the process flow will be the same with
> all three methods.  Parse it, sanitize it, store it.
>
> Hopefully that explains a little more.
>
> Jim

In short, are you looking for a program which listens on a serial port
and a network port at the same time, and places any data received from
either into a database?


Regards,

Torben

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



[PHP] Rounding down?

2009-08-22 Thread Ron Piggott
Is there a way to round down to the nearest 50?

Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 
would be 450; 500 to 549 would be 500, etc?

The original number of subscribers is from a mySQL query and changes each day.  
I am trying to present a factual statement: "There have been over ### 
subscribers in 2009" type of scenereo.

Ron

[PHP] Re: Rounding down?

2009-08-22 Thread David Robley
Ron Piggott wrote:

> Is there a way to round down to the nearest 50?
> 
> Example: Any number between 400 and 449 I would 400 to be displayed; 450
> to 499 would be 450; 500 to 549 would be 500, etc?
> 
> The original number of subscribers is from a mySQL query and changes each
> day.  I am trying to present a factual statement: "There have been over
> ### subscribers in 2009" type of scenereo.
> 
> Ron

Modulus - http://php.net/manual/en/language.operators.arithmetic.php



Cheers
-- 
David Robley

Oxymoron: Split level.
Today is Prickle-Prickle, the 15th day of Bureaucracy in the YOLD 3175. 


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



Re: [PHP] Invoking functions stored in a separate directory?

2009-08-22 Thread David Robley
Clancy wrote:

> $ok = include (HOST_PATH.'/Halla.php');

Because you are assigning the result of the include to a variable. Try 

include (HOST_PATH.'/Halla.php');

and it will work as you expect. And similarly for 

define ('HOST_PATH','../Engine');


Cheers
-- 
David Robley

Dynamic linking error: Your mistake is now everywhere.
Today is Prickle-Prickle, the 15th day of Bureaucracy in the YOLD 3175. 


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



Re: [PHP] Rounding down?

2009-08-22 Thread Richard Heyes
Hi,

> Is there a way to round down to the nearest 50?
>
> Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 
> 499 would be 450; 500 to 549 would be 500, etc?

Off the top of my head: divide the number by 50, run floor() on the
result, then times it by 50.

1. 449 / 50 = 9.whatever
2. floor(9.whatever) = 9
3. 9 * 50 = 450

-- 
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
Lots of PHP and Javascript code - http://www.phpguru.org

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



Re: [PHP] Rounding down?

2009-08-22 Thread Ashley Sheridan
On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:
> Hi,
> 
> > Is there a way to round down to the nearest 50?
> >
> > Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 
> > 499 would be 450; 500 to 549 would be 500, etc?
> 
> Off the top of my head: divide the number by 50, run floor() on the
> result, then times it by 50.
> 
> 1. 449 / 50 = 9.whatever
> 2. floor(9.whatever) = 9
> 3. 9 * 50 = 450
> 
> -- 
> Richard Heyes
> HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
> Lots of PHP and Javascript code - http://www.phpguru.org
> 

It should be round() and not floor().

449 / 50 = 8.98
floor(8.98) = 8
8 * 50 = 400

round(8.98) = 9
9 * 50 = 450



Thanks,
Ash
http://www.ashleysheridan.co.uk




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



Re: [PHP] Rounding down?

2009-08-22 Thread Ron Piggott

Thanks; Amazing.  Ron

- Original Message - 
From: "Ashley Sheridan" 

To: "Richard Heyes" 
Cc: "Ron Piggott" ; 
Sent: Saturday, August 22, 2009 9:02 AM
Subject: Re: [PHP] Rounding down?



On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:

Hi,

> Is there a way to round down to the nearest 50?
>
> Example: Any number between 400 and 449 I would 400 to be displayed; 
> 450 to 499 would be 450; 500 to 549 would be 500, etc?


Off the top of my head: divide the number by 50, run floor() on the
result, then times it by 50.

1. 449 / 50 = 9.whatever
2. floor(9.whatever) = 9
3. 9 * 50 = 450

--
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
Lots of PHP and Javascript code - http://www.phpguru.org



It should be round() and not floor().

449 / 50 = 8.98
floor(8.98) = 8
8 * 50 = 400

round(8.98) = 9
9 * 50 = 450



Thanks,
Ash
http://www.ashleysheridan.co.uk










No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.409 / Virus Database: 270.13.64/2319 - Release Date: 08/22/09 
06:06:00



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



Re: [PHP] Rounding down?

2009-08-22 Thread דניאל דנון
I also wrote a function for that,

function round_to($number, $increments) {
$increments = 1 / $increments;
return (round($number * $increments) / $increments);
}

(Also published on php manual - round() )


On Sat, Aug 22, 2009 at 4:11 PM, Ron Piggott wrote:

> Thanks; Amazing.  Ron
>
> - Original Message - From: "Ashley Sheridan" <
> a...@ashleysheridan.co.uk>
> To: "Richard Heyes" 
> Cc: "Ron Piggott" ;  >
> Sent: Saturday, August 22, 2009 9:02 AM
> Subject: Re: [PHP] Rounding down?
>
>
>  On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:
>>
>>> Hi,
>>>
>>> > Is there a way to round down to the nearest 50?
>>> >
>>> > Example: Any number between 400 and 449 I would 400 to be displayed; >
>>> 450 to 499 would be 450; 500 to 549 would be 500, etc?
>>>
>>> Off the top of my head: divide the number by 50, run floor() on the
>>> result, then times it by 50.
>>>
>>> 1. 449 / 50 = 9.whatever
>>> 2. floor(9.whatever) = 9
>>> 3. 9 * 50 = 450
>>>
>>> --
>>> Richard Heyes
>>> HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
>>> Lots of PHP and Javascript code - http://www.phpguru.org
>>>
>>>
>> It should be round() and not floor().
>>
>> 449 / 50 = 8.98
>> floor(8.98) = 8
>> 8 * 50 = 400
>>
>> round(8.98) = 9
>> 9 * 50 = 450
>>
>>
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
>>
>
>
> 
>
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.409 / Virus Database: 270.13.64/2319 - Release Date: 08/22/09
> 06:06:00
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Use ROT26 for best security


Re: [PHP] Rounding down?

2009-08-22 Thread Richard Heyes
Hi,

> It should be round() and not floor().
>
> 449 / 50 = 8.98
> floor(8.98) = 8
> 8 * 50 = 400
>
> round(8.98) = 9
> 9 * 50 = 450

Not based on the examples given:

> Example: Any number between 400 and 449 I would 400 to be displayed

-- 
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
Lots of PHP and Javascript code - http://www.phpguru.org

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



[PHP] preg_replace anything that isn't WORD

2009-08-22 Thread דניאל דנון
Lets assume I have the string "cats i  saw a cat and a dog"
i want to strip everything except "cat" and "dog" so the result will be
"catcatdog",
using preg_replace.


I've tried something like /[^(dog|cat)]+/ but no success

What should I do?

-- 
Use ROT26 for best security


[PHP] Re: How to download and configure php mvc website locally

2009-08-22 Thread Shawn McKenzie
Sumit Sharma wrote:
> Hi all,
> The site I have download was developed using cake php. Now when trying to
> access the website it is showing a blank page. As Sudheer suggested I went
> to error log and noted down the errors there, which are as follows:
> 
> [Thu Aug 20 14:10:16 2009] [error] [client 127.0.0.1] File does not exist:
> F:/Rabin/xampp/htdocs/favicon.ico
> 
> I think there is no file called favicon.ico but how to create this file. Is
> this something related to cakephp configuration.
> 
> Regards,
> Sumit
> 
> 
> 
> On Wed, Aug 19, 2009 at 8:25 PM, Sumit Sharma  wrote:
> 
>> Hi,
>> Please help as I have downloaded a php website for my client which is
>> developed using model view controller pattern. Now when I am accessing this
>> site locally it is not showing any thing. Please tell me the what's the
>> problem
>>
>> Thanks,
>>   Sumit Sharma
>>
> 

Have you installed the CakePHP framework in your include path?

-- 
Thanks!
-Shawn
http://www.spidean.com

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



Re: [PHP] preg_replace anything that isn't WORD

2009-08-22 Thread Jonathan Tapicer
Negating specific words with regexes isn't a good practice (see a deep
discussion here: http://www.perlmonks.org/?node_id=588315), in your
case I would resolve it like this:



That will output:

array(5) {
  [0]=>
  string(3) "cat"
  [1]=>
  string(10) "s i saw a "
  [2]=>
  string(3) "cat"
  [3]=>
  string(7) " and a "
  [4]=>
  string(3) "dog"
}

Then you just have to go through the result array of preg_split and
concatenate every "cat" and "dog".

Regards,

Jonathan

On Sat, Aug 22, 2009 at 12:32 PM, דניאל דנון wrote:
> Lets assume I have the string "cats i  saw a cat and a dog"
> i want to strip everything except "cat" and "dog" so the result will be
> "catcatdog",
> using preg_replace.
>
>
> I've tried something like /[^(dog|cat)]+/ but no success
>
> What should I do?
>
> --
> Use ROT26 for best security
>

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



Re: [PHP] Rounding down?

2009-08-22 Thread Richard Heyes
Hi,

> ...

A little modification:

= 50 ? '50' : '00'));
}

echo myRound(449) . ''; // 400
echo myRound(450) . ''; // 450
echo myRound(356) . ''; // 350
echo myRound(79) . '';  // 50
?>

PS I haven't checked if there's a PHP function for this.

-- 
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
Lots of PHP and Javascript code - http://www.phpguru.org

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



[PHP] array() returns something weird

2009-08-22 Thread Szczepan Hołyszewski
Hello!

I am almost certain I am hitting some kind of bug. All of a sudden, array() 
stops returning an empty array and starts returning something weird. The weird 
thing behaves as NULL in most circumstances (e.g. gettype() says NULL), 
except:

$foo=array();  // <-- weird thing returned
$foo[]="bar";

causes "Fatal error: [] operator not supported for strings", which is different 
from the regular behavior of:

$foo=null;
$foo[]="bar";  // <-- $foo simply becomes an array

The problem is not limited to one place in code, and indeed before the fatal 
caused by append-assignment I get several warnings like "array_diff_key(): 
Argument #1 is not an array", where the offending argument receives a result of 
array().

The effect is not random, i.e. it always breaks identically when the same 
script processes the same data. However I was so far unable to create a 
minimal test case that triggers the bug. My script is rather involved, and 
here are some things it uses:

 - Exceptions
 - DOM to-fro SimpleXML
 - lots of multi-level output buffering

Disabling Zend Optimizer doesn't help. Disabling Zend Memory Manager is 
apparently impossible. Memory usage is below 10MB out of 128MB limit.

Any similar experiences? Ideas what to check for? Workarounds?

From phpinfo():

PHP Version:
5.2.9 (can't easily upgrade - shared host)

System:
FreeBSD 7.1-RELEASE-p4 FreeBSD 7.1-RELEASE-p4 #0: Wed Apr 15 15:48:43 UTC 2009  
amd64

Configure Command:
'./configure' '--enable-bcmath' '--enable-calendar' '--enable-dbase' '--enable-
exif' '--enable-fastcgi' '--enable-force-cgi-redirect' '--enable-ftp' '--
enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-
maintainer-zts' '--enable-mbstring' '--enable-pdo=shared' '--enable-safe-mode' 
'--enable-soap' '--enable-sockets' '--enable-ucd-snmp-hack' '--enable-wddx' 
'--enable-zend-multibyte' '--enable-zip' '--prefix=/usr' '--with-bz2' '--with-
curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr/local' '--
with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-
imap-ssl=/usr/local' '--with-jpeg-dir=/usr/local' '--with-libexpat-
dir=/usr/local' '--with-libxml-dir=/opt/xml2' '--with-libxml-dir=/opt/xml2/' 
'--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mime-magic' 
'--with-mysql=/usr/local' '--with-mysql-sock=/tmp/mysql.sock' '--with-
mysqli=/usr/local/bin/mysql_config' '--with-openssl=/usr/local' '--with-
openssl-dir=/usr/local' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' 
'--with-pgsql=/usr/local' '--with-pic' '--with-png-dir=/usr/local' '--with-
pspell' '--with-snmp' '--with-sqlite=shared' '--with-tidy=/opt/tidy/' '--with-
ttf' '--with-xmlrpc' '--with-xpm-dir=/usr/local' '--with-xsl=/opt/xslt/' '--
with-zlib' '--with-zlib-dir=/usr'

Thanks in advance,
Szczepan Holyszewski

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



Re: [PHP] array() returns something weird

2009-08-22 Thread Lars Torben Wilson
2009/8/22 Szczepan Hołyszewski :
> Hello!
>
> I am almost certain I am hitting some kind of bug. All of a sudden, array()
> stops returning an empty array and starts returning something weird. The weird
> thing behaves as NULL in most circumstances (e.g. gettype() says NULL),
> except:
>
>        $foo=array();      // <-- weird thing returned
>        $foo[]="bar";
>
> causes "Fatal error: [] operator not supported for strings", which is 
> different
> from the regular behavior of:

Hi there,

Without seeing the actual code, it's hard to say what the problem is.
However, I'd be pretty surprised if you've actually run into a bug in
PHP--I would first suspect a bug in your code. No offense
intended--that's just how it usually plays out. :)

What it looks like to me is that something is causing $foo to be a
string before the '$foo[] = "bar";' line is encountered. What do you
get if you put a gettype($foo); just before that line?

>        $foo=null;
>        $foo[]="bar";      // <-- $foo simply becomes an array
>
> The problem is not limited to one place in code, and indeed before the fatal
> caused by append-assignment I get several warnings like "array_diff_key():
> Argument #1 is not an array", where the offending argument receives a result 
> of
> array().

This would appear to support my suspicion, but try inserting the
gettype($foo) (or better, var_export($foo);) just before one of the
lines which triggers the error, and post the results.

Can you post the code in a .zip file or online somewhere? If not,
that's cool, but it will probably make it harder to help you track it
down if you can't.


Regards,

Torben

> The effect is not random, i.e. it always breaks identically when the same
> script processes the same data. However I was so far unable to create a
> minimal test case that triggers the bug. My script is rather involved, and
> here are some things it uses:
>
>  - Exceptions
>  - DOM to-fro SimpleXML
>  - lots of multi-level output buffering
>
> Disabling Zend Optimizer doesn't help. Disabling Zend Memory Manager is
> apparently impossible. Memory usage is below 10MB out of 128MB limit.
>
> Any similar experiences? Ideas what to check for? Workarounds?
>
> From phpinfo():
>
> PHP Version:
> 5.2.9 (can't easily upgrade - shared host)
>
> System:
> FreeBSD 7.1-RELEASE-p4 FreeBSD 7.1-RELEASE-p4 #0: Wed Apr 15 15:48:43 UTC 2009
> amd64
>
> Configure Command:
> './configure' '--enable-bcmath' '--enable-calendar' '--enable-dbase' 
> '--enable-
> exif' '--enable-fastcgi' '--enable-force-cgi-redirect' '--enable-ftp' '--
> enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-
> maintainer-zts' '--enable-mbstring' '--enable-pdo=shared' '--enable-safe-mode'
> '--enable-soap' '--enable-sockets' '--enable-ucd-snmp-hack' '--enable-wddx'
> '--enable-zend-multibyte' '--enable-zip' '--prefix=/usr' '--with-bz2' '--with-
> curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr/local' '--
> with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-
> imap-ssl=/usr/local' '--with-jpeg-dir=/usr/local' '--with-libexpat-
> dir=/usr/local' '--with-libxml-dir=/opt/xml2' '--with-libxml-dir=/opt/xml2/'
> '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mime-magic'
> '--with-mysql=/usr/local' '--with-mysql-sock=/tmp/mysql.sock' '--with-
> mysqli=/usr/local/bin/mysql_config' '--with-openssl=/usr/local' '--with-
> openssl-dir=/usr/local' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared'
> '--with-pgsql=/usr/local' '--with-pic' '--with-png-dir=/usr/local' '--with-
> pspell' '--with-snmp' '--with-sqlite=shared' '--with-tidy=/opt/tidy/' '--with-
> ttf' '--with-xmlrpc' '--with-xpm-dir=/usr/local' '--with-xsl=/opt/xslt/' '--
> with-zlib' '--with-zlib-dir=/usr'
>
> Thanks in advance,
> Szczepan Holyszewski
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

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



[PHP] Re: preg_replace anything that isn't WORD

2009-08-22 Thread Shawn McKenzie
דניאל דנון wrote:
> Lets assume I have the string "cats i  saw a cat and a dog"
> i want to strip everything except "cat" and "dog" so the result will be
> "catcatdog",
> using preg_replace.
> 
> 
> I've tried something like /[^(dog|cat)]+/ but no success
> 
> What should I do?
> 

Capture everything but only replace the backreference for the words:

$r = preg_replace('#.*?(cat|dog).*?#', '\1', $s);

-- 
Thanks!
-Shawn
http://www.spidean.com

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



Re: [PHP] array() returns something weird

2009-08-22 Thread Szczepan Hołyszewski
> What it looks like to me is that something is causing $foo to be a
> string before the '$foo[] = "bar";' line is encountered. What do you
> get if you put a gettype($foo); just before that line?
>
> >$foo=null;
> >$foo[]="bar";  // <-- $foo simply becomes an array

NULL. That is the problem. I _did_ put a gettype($foo) before the actual line.

OK, here are exact four lines of my code:

$ret=array();
foreach(self::$_allowed as $r => $a)
if ($a)
$ret[]=$r;

As you can see, there is not a shred of a chance for $ret to become something 
other than empty array between initialization and the last line in the above 
snippet which causes the fatal errror. There's no __staticGet in 5.2.9, so 
self::$_allowed cannot have side effects.

Secondly, the above code starts failing after it has executed successfully 
dozens of times (and yes, the last line _does_ get executed; in fact self::
$_allowed contains configuration information that doesn't change at runtime).

Thirdly...

> > The problem is not limited to one place in code, and indeed before the
> > fatal caused by append-assignment I get several warnings like
> > "array_diff_key(): Argument #1 is not an array", where the offending
> > argument receives a result of array().
>
> This would appear to support my suspicion, but try inserting the
> gettype($foo) (or better, var_export($foo);) just before one of the
> lines which triggers the error, and post the results.

No, I don't think it supports your suspicion. Conversely, it indicates that 
once array() returns a strangelet, it starts returning strangelets all over 
the place. Initially it only triggers warnings but eventually one of the 
returned strangelets is used in a way that triggers a fatal error.

As per your request:

//at the beginning of the script:

$GLOBALS['offending_line_execution_count']=0;

// /srv/home/[munged]/public_html/scripts/common.php line 161 and on
// instrumented as per your request:

public static function GetAllowed() {

if (debug_mode()) echo 
++$GLOBALS['offending_line_execution_count']."";
$ret=array();
if (debug_mode()) echo var_export($ret)."";
foreach(self::$_allowed as $r => $a)
if ($a)
$ret[]=$r;

if (self::$_allowEmpty) $ret[]="";
return $ret;
}

Output tail:
---
28
array ( )
29
array ( )
30
array ( )
31
array ( )
32
array ( )

Warning: array_diff_key() [function.array-diff-key]: Argument #1 is not an 
array 
in /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 350

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 351

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in 
/srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 357

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28
33
NULL

Fatal error: [] operator not supported for strings in 
/srv/home/u80959ue/public_html/scripts/common.php on line 168
--

The warnings come from other uses of array().

But wait! There is this invocation of debug_mode() between initialization of 
$ret var_export. Let's factor it out to be safe:

$debugmode=debug_mode();
if ($debugmode) echo 
++$GLOBALS['offending_line_execution_count']."";
$ret=array();
if ($debugmode) echo var_export($ret)."";

And now the output ends with:


Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28
33

Fatal error: [] operator not supported for strings in 
/srv/home/u80959ue/public_html/scripts/common.php on line 169


No NULL after 33? What the heck is going on? Does array() now return something 
that var_exports to an empty string, or does it destroy local variables? Let's 
see:

if ($debugmode) echo var_export($ret).""; else echo 
"WTF?!?!?";

And the output:

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28
33
WTF?!?!?

Fatal error: [] operator not supported for strings in 
/srv/home/u80959ue/public_html/scripts/common.php on line 169
---

Indeed, the use of array(), once it starts misbehaving, wreaks havoc in the 
local scope (poss

Re: [PHP] array() returns something weird

2009-08-22 Thread Lars Torben Wilson
2009/8/22 Szczepan Hołyszewski :
>> What it looks like to me is that something is causing $foo to be a
>> string before the '$foo[] = "bar";' line is encountered. What do you
>> get if you put a gettype($foo); just before that line?
>>
>> >        $foo=null;
>> >        $foo[]="bar";      // <-- $foo simply becomes an array
>
> NULL. That is the problem. I _did_ put a gettype($foo) before the actual line.
>
> OK, here are exact four lines of my code:
>
>        $ret=array();
>        foreach(self::$_allowed as $r => $a)
>                if ($a)
>                        $ret[]=$r;
>
> As you can see, there is not a shred of a chance for $ret to become something
> other than empty array between initialization and the last line in the above
> snippet which causes the fatal errror. There's no __staticGet in 5.2.9, so
> self::$_allowed cannot have side effects.
>
> Secondly, the above code starts failing after it has executed successfully
> dozens of times (and yes, the last line _does_ get executed; in fact self::
> $_allowed contains configuration information that doesn't change at runtime).
>
> Thirdly...
>
>> > The problem is not limited to one place in code, and indeed before the
>> > fatal caused by append-assignment I get several warnings like
>> > "array_diff_key(): Argument #1 is not an array", where the offending
>> > argument receives a result of array().
>>
>> This would appear to support my suspicion, but try inserting the
>> gettype($foo) (or better, var_export($foo);) just before one of the
>> lines which triggers the error, and post the results.
>
> No, I don't think it supports your suspicion. Conversely, it indicates that
> once array() returns a strangelet, it starts returning strangelets all over
> the place. Initially it only triggers warnings but eventually one of the
> returned strangelets is used in a way that triggers a fatal error.
>
> As per your request:
>
>        //at the beginning of the script:
>
>        $GLOBALS['offending_line_execution_count']=0;
>
>        // /srv/home/[munged]/public_html/scripts/common.php line 161 and on
>        // instrumented as per your request:
>
>        public static function GetAllowed() {
>
>                if (debug_mode()) echo 
> ++$GLOBALS['offending_line_execution_count']."";
>                $ret=array();
>                if (debug_mode()) echo var_export($ret)."";
>                foreach(self::$_allowed as $r => $a)
>                        if ($a)
>                                $ret[]=$r;
>
>                if (self::$_allowEmpty) $ret[]="";
>                return $ret;
>        }
>
> Output tail:
> ---
> 28
> array ( )
> 29
> array ( )
> 30
> array ( )
> 31
> array ( )
> 32
> array ( )
>
> Warning: array_diff_key() [function.array-diff-key]: Argument #1 is not an 
> array
> in /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 350
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 351
>
> Warning: array_merge() [function.array-merge]: Argument #2 is not an array in
> /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 357
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
> 33
> NULL
>
> Fatal error: [] operator not supported for strings in
> /srv/home/u80959ue/public_html/scripts/common.php on line 168
> --
>
> The warnings come from other uses of array().
>
> But wait! There is this invocation of debug_mode() between initialization of
> $ret var_export. Let's factor it out to be safe:
>
>                $debugmode=debug_mode();
>                if ($debugmode) echo 
> ++$GLOBALS['offending_line_execution_count']."";
>                $ret=array();
>                if ($debugmode) echo var_export($ret)."";
>
> And now the output ends with:
>
> 
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
> 33
>
> Fatal error: [] operator not supported for strings in
> /srv/home/u80959ue/public_html/scripts/common.php on line 169
> 
>
> No NULL after 33? What the heck is going on? Does array() now return something
> that var_exports to an empty string, or does it destroy local variables? Let's
> see:
>
>                if ($debugmode) echo var_export($ret).""; else echo 
> "WTF?!?!?";
>
> And the output:
> 
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
> 33
> WTF?!?!?
>
> Fatal error: [] operator not supported 

[PHP] Re: preg_replace anything that isn't WORD

2009-08-22 Thread Shawn McKenzie
Didn't seem to make it the first time.

Shawn McKenzie wrote:
> דניאל דנון wrote:
>> Lets assume I have the string "cats i  saw a cat and a dog"
>> i want to strip everything except "cat" and "dog" so the result will be
>> "catcatdog",
>> using preg_replace.
>>
>>
>> I've tried something like /[^(dog|cat)]+/ but no success
>>
>> What should I do?
>>

Capture everything but only replace the backreference for the words:

$r = preg_replace('#.*?(cat|dog).*?#', '\1', $s);


-Shawn

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



Re: [PHP] array() returns something weird

2009-08-22 Thread Ralph Deffke
well, when I saw ur post I got immediately the thought I would bed it has to
do with some stuff of $this or self.

I did play arround a bit with class creation the last days and yes, with
using self parent and $this I did put the HTTPPD in unstable and sometimes
it died without beeing able to send any error.

well this doesn't help very mutch.

I have two point:

(1)ur code is ( sorry ) lazy written, invest the brackets !! ur code writing
is predestinated for that type of error. shooting variable types arround by
pulling out of foreach loops,  if's,  is typical.

(2) using static variables are known for type missmatch errors just anything
has acces to them even if the containing class is not instantinated. many
dirty things can happen unless of corse they are not private.

further sugestions: check if you work on ur arrays with functions returning
array on success but false on fail or something like that. also a typical
source for that type of error

are u using magic __set ? I ran into a type change as well with it

good luck

ralph_def...@yahoo.de


"Szczepan Holyszewski"  wrote in message
news:200908222152.55846.webmas...@strefarytmu.pl...
> > What it looks like to me is that something is causing $foo to be a
> > string before the '$foo[] = "bar";' line is encountered. What do you
> > get if you put a gettype($foo); just before that line?
> >
> > >$foo=null;
> > >$foo[]="bar";  // <-- $foo simply becomes an array
>
> NULL. That is the problem. I _did_ put a gettype($foo) before the actual
line.
>
> OK, here are exact four lines of my code:
>
> $ret=array();
> foreach(self::$_allowed as $r => $a)
> if ($a)
> $ret[]=$r;
>
> As you can see, there is not a shred of a chance for $ret to become
something
> other than empty array between initialization and the last line in the
above
> snippet which causes the fatal errror. There's no __staticGet in 5.2.9, so
> self::$_allowed cannot have side effects.
>
> Secondly, the above code starts failing after it has executed successfully
> dozens of times (and yes, the last line _does_ get executed; in fact
self::
> $_allowed contains configuration information that doesn't change at
runtime).
>
> Thirdly...
>
> > > The problem is not limited to one place in code, and indeed before the
> > > fatal caused by append-assignment I get several warnings like
> > > "array_diff_key(): Argument #1 is not an array", where the offending
> > > argument receives a result of array().
> >
> > This would appear to support my suspicion, but try inserting the
> > gettype($foo) (or better, var_export($foo);) just before one of the
> > lines which triggers the error, and post the results.
>
> No, I don't think it supports your suspicion. Conversely, it indicates
that
> once array() returns a strangelet, it starts returning strangelets all
over
> the place. Initially it only triggers warnings but eventually one of the
> returned strangelets is used in a way that triggers a fatal error.
>
> As per your request:
>
> //at the beginning of the script:
>
> $GLOBALS['offending_line_execution_count']=0;
>
> // /srv/home/[munged]/public_html/scripts/common.php line 161 and on
> // instrumented as per your request:
>
> public static function GetAllowed() {
>
> if (debug_mode()) echo
++$GLOBALS['offending_line_execution_count']."";
> $ret=array();
> if (debug_mode()) echo var_export($ret)."";
> foreach(self::$_allowed as $r => $a)
> if ($a)
> $ret[]=$r;
>
> if (self::$_allowEmpty) $ret[]="";
> return $ret;
> }
>
> Output tail:
> ---
> 28
> array ( )
> 29
> array ( )
> 30
> array ( )
> 31
> array ( )
> 32
> array ( )
>
> Warning: array_diff_key() [function.array-diff-key]: Argument #1 is not an
array
> in /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 350
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 351
>
> Warning: array_merge() [function.array-merge]: Argument #2 is not an array
in
> /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 357
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
>
> Warning: Invalid argument supplied for foreach() in
> /srv/home/u80959ue/public_html/scripts/common.php on line 28
> 33
> NULL
>
> Fatal error: [] operator not supported for strings in
> /srv/home/u80959ue/public_html/scripts/common.php on line 168
> --
>
> The warnings come from other uses of array().
>
> But wait! There is this invocation of debug_mode() between initialization
of
> $ret var_export. Let's factor it out to be safe:
>
> $debugmode=debug_mode();
> if ($debugmode) echo ++$GLOBALS['offending_line_execution_count']."";
> $ret=array();
> if ($debugmode) echo var_export($ret)."";
>
> And now

Re: [PHP] array() returns something weird

2009-08-22 Thread Szczepan Hołyszewski
> Hm. . .it does look odd. Searching the bugs database at
> http://bugs.php.net does turn up one other report (at
> http://bugs.php.net/bug.php?id=47870 ) of array() returning NULL in
> certain hard-to-duplicate circumstances on FreeBSD,

Yes, I found it even before posting here, but I wasn't sure whether to file a 
new report or comment under this one. If your intuition is that these bugs are 
related, then I will do the latter. Thank you for your attention.

> I don't suppose you have a development environment on another
> machine where you can test another version of PHP?

Assuming you mean a FreeBSD environment, nope :( but I will try on Linux 
tomorrow.

Regards,
Szczepan Holyszewski


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



Re: [PHP] array() returns something weird

2009-08-22 Thread Lars Torben Wilson
2009/8/22 Szczepan Hołyszewski :
>> Hm. . .it does look odd. Searching the bugs database at
>> http://bugs.php.net does turn up one other report (at
>> http://bugs.php.net/bug.php?id=47870 ) of array() returning NULL in
>> certain hard-to-duplicate circumstances on FreeBSD,
>
> Yes, I found it even before posting here, but I wasn't sure whether to file a
> new report or comment under this one. If your intuition is that these bugs are
> related, then I will do the latter. Thank you for your attention.

Well, the only things I'm basing my suspicion on are the nature of the
problem, the OS similarity and the fact that it seems to be difficult
to reproduce the problem reliably. The major problem with this guess
is that the original bug report does state that the bug did not show
up under 5.2.

>> I don't suppose you have a development environment on another
>> machine where you can test another version of PHP?
>
> Assuming you mean a FreeBSD environment, nope :( but I will try on Linux
> tomorrow.

OK. I do think (as I'm sure you know) that the best test would be in a
matching environment (since the result was reported to be different
under Linux for that bug), but of course that's not always realistic.

> Regards,
> Szczepan Holyszewski

I hope your problem can be resolved. If it does turn out to be a bug
in PHP I hope that will be enough to convince your host to upgrade.


Regards,

Torben

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



Re: [PHP] Invoking functions stored in a separate directory?

2009-08-22 Thread Clancy
On Sat, 22 Aug 2009 20:37:17 +0930, robl...@aapt.net.au (David Robley) wrote:

>Clancy wrote:
>
>> $ok = include (HOST_PATH.'/Halla.php');
>
>Because you are assigning the result of the include to a variable. Try 
>
>include (HOST_PATH.'/Halla.php');
>
>and it will work as you expect. And similarly for 
>
>define ('HOST_PATH','../Engine');

Thanks. But no; that's not the answer.  'Include', like any other function, 
returns a
result; true if the operation was successful, and false if it failed. I was 
using this to
verify that the 'include' operation had been successful.

The more I thought about it last night, the more dissatisfied I became with the 
notions
that 'include' could work differently in different circumstances, and also that 
defining a
path could alter the way it worked.

I did some more tests this morning, and eventually established that my 
confusion was the
result of two different effects.

The first was that I find the PHP syntax rather picky,  and I rely on error 
messages to
alert me to my frequent errors, but apparently all error messages are turned 
off on the
remote system, and if anything is wrong all I get is a blank screen.

The second is that although I use Dreamweaver as an editor, and it has 
moderately good
colour coding which indicate most errors, I am partially red green colour blind 
and tend
not to notice the erroneous colours.

I repeated my tests this morning and eventually managed to establish that

i. there is no difference in the behaviour of include between the local 
and remote
systems, and

ii. contrary to Arno's advice, include ../Engine/Main_prog.php; works 
just the
same as include ENGINEPATH."Main_prog.php";

So now I can get on with uploading my program!

Thanks,

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




Re: [PHP] Rounding down?

2009-08-22 Thread Clancy
On Sat, 22 Aug 2009 14:02:58 +0100, a...@ashleysheridan.co.uk (Ashley Sheridan) 
wrote:

>On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote:
>> Hi,
>> 
>> > Is there a way to round down to the nearest 50?
>> >
>> > Example: Any number between 400 and 449 I would 400 to be displayed; 450 
>> > to 499 would be 450; 500 to 549 would be 500, etc?
>> 
>> Off the top of my head: divide the number by 50, run floor() on the
>> result, then times it by 50.
>> 
>> 1. 449 / 50 = 9.whatever
>> 2. floor(9.whatever) = 9
>> 3. 9 * 50 = 450
>> 
>> -- 
>> Richard Heyes
>> HTML5 graphing: RGraph - www.rgraph.net (updated 8th August)
>> Lots of PHP and Javascript code - http://www.phpguru.org
>> 
>
>It should be round() and not floor().
>
>449 / 50 = 8.98
>floor(8.98) = 8
>8 * 50 = 400
>
>round(8.98) = 9
>9 * 50 = 450
>
Definitely floor or int, not round.

Round 0.5-1.499 -> 1
Floor 1.0-1.999 -> 1

And if you really want to be accurate  you should allow for decimal conversion 
errors in
any operation involving floating point numbers.  Otherwise you cannot rely on 
(say) 50 not
being represented as 49.99, so that int or floor will give 49. In 
something like
this I usually add half of the precision I want to work to:

eg: $answer = 50* (int) (($x +0.5)/50);

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



Re: [PHP] Re: How do I extract link text from anchor tag as well as the URL from the "href" attribute

2009-08-22 Thread Raymond Irving
Hello,

You might also want to try using the Raxan framework:

require_once 'raxan/pdi/gateway.php';

$page = new RichWebPage('page.html');
echo $page['a']->text(); // this will get the text betwen the a tag
 
To get the image element use:

$elm = $page['a img']->node(0);

You can download Raxan here:
http://raxanpdi.com/downloads.html

__
Raymond Irving

--- On Sat, 8/22/09, Manuel Lemos  wrote:

From: Manuel Lemos 
Subject: [PHP] Re: How do I extract link text from anchor tag as well as the 
URL from the "href" attribute
To: "chrysanhy" 
Cc: php-general@lists.php.net
Date: Saturday, August 22, 2009, 1:07 AM

Hello,

on 08/16/2009 04:33 AM chrysanhy said the following:
> I have the following code to extract the URLs from the anchor tags of an
> HTML page:
> 
> $html = new DOMDocument();
> $htmlpage->loadHtmlFile($location);
> $xpath = new DOMXPath($htmlpage);
> $links = $xpath->query( '//a' );
> foreach ($links as $link)
> { $int_url_list[$i++] = $link->getAttribute( 'href' ) . "\n"; }
> 
> If I have a link http://X.com";>, how do I extract the
> corresponding  which is displayed to the user as the text of the link
> (if it's an image tag, I would like a DOMElement for that).
> Thanks

You may want to try this HTML parser class that comes with filter class
and an example script named test_get_html_links.php  that does exactly
what you ask.

http://www.phpclasses.org/secure-html-filter

-- 

Regards,
Manuel Lemos

Find and post PHP jobs
http://www.phpclasses.org/jobs/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

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



Re: [PHP] Is there limitation for switch case: argument's value?

2009-08-22 Thread Keith

Thanks! Torben.
I got the point now and it works! :-)
I'm doing this because the statements of each cases is quite long, and I 
wish to have minimum coding without repetition.



"Lars Torben Wilson"  wrote in message 
news:36d4833b0908202323p3c858b5fn6a1d6775aa7f8...@mail.gmail.com...

2009/8/20 Keith :

Hi,
I encounter a funny limitation here with switch case as below:
The value for $sum is worked as expected for 1 to 8, but not for 0.
When the $sum=0, the first case will be return, which is "sum=8".
Is there any limitation / rules for switch case? Thanks for advice!

Keith


Hi Keith,

Try replacing 'switch($sum)' with 'switch(true)'.

Note that unless you have very good reasons for using a switch
statement like this, and know exactly why you're doing it, it's often
better just to use it in the normal fashion. i.e.:

  switch ($sum)
   {
   case 8:
   break;
   case 7:
   case 6:
   break;
   case 2:
   case 1:
   break;
   case 0:
   break;
default:
   break;
   }

Some people like the syntax you've presented but honestly, there's
usually a better way to do it.

This is also somewhat faster too, although you may only notice the
difference in very tight loops where you're counting every nanosecond.


Regards,

Torben


$sum=0;
switch($sum)
{
  case ($sum==8):
  echo "sum=8";
  break;
  case ($sum==7 || $sum==6):
  echo "sum=7 or 6";
  break;
  case ($sum==2 || $sum==1):
  echo "sum=2 or 1";
  break;
  case 0:
  echo "sum=0";
  break;
  default:
  echo "sum=3/4/5";
  break;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




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



Re: [PHP] Is there limitation for switch case: argument's value?

2009-08-22 Thread Keith

Thanks! Adam.
It works now!
Actually I thought that switch block is compatible with if/elseif/else block 
but more efficient if the cases is >3.
Because I like to have short coding without repetition, so I group the 
similar cases together.
Thanks for your explanation! I know where is my mistake already and I've 
learnt a lesson today!


"Adam Randall"  wrote in message 
news:b6d6f8360908202319n240bba5al6c02edb6e890b...@mail.gmail.com...

I've never understood why people use switch statements like this as in
reality you are using it more like an if/else block, but anyway.

The reason why your code is not working is that you are passing into
the switch the value of 0, or false, which means that when any of
those $sum == N conditionals comes up as false (say $sum == 8 ), then
that is what is returned because they match up. In PHP's eyes, 0 ==
false in switch statements.

To fix your code, change the switch( $sum ) to switch( true ):

switch( true )
{
case ($sum == 8):
echo "sum=8\n";
break;
case ($sum == 7 || $sum == 6):
echo "sum=7 or 6\n";
break;
case ($sum == 2 || $sum == 1):
echo "sum=2 or 1\n";
break;
case ($sum == 0):
echo "sum=0\n";
break;
default:
echo "sum=3/4/5\n";
break;
}

Or, write your switch like this:

switch( $sum )
{
case 8:
echo "sum=8\n";
break;
case 6:
case 7:
echo "sum=7 or 6\n";
break;
case 1:
case 2:
echo "sum=2 or 1\n";
break;
case 0:
echo "sum=0\n";
break;
default:
echo "sum=3/4/5\n";
break;
}

Regards,

Adam.

On Thu, Aug 20, 2009 at 8:40 PM, Keith wrote:

Hi,
I encounter a funny limitation here with switch case as below:
The value for $sum is worked as expected for 1 to 8, but not for 0.
When the $sum=0, the first case will be return, which is "sum=8".
Is there any limitation / rules for switch case? Thanks for advice!

Keith

$sum=0;
switch($sum)
{
  case ($sum==8):
  echo "sum=8";
  break;
  case ($sum==7 || $sum==6):
  echo "sum=7 or 6";
  break;
  case ($sum==2 || $sum==1):
  echo "sum=2 or 1";
  break;
  case 0:
  echo "sum=0";
  break;
  default:
  echo "sum=3/4/5";
  break;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






--
Adam Randall
http://www.xaren.net
AIM: blitz574 



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



Re: [PHP] Is there limitation for switch case: argument's value?

2009-08-22 Thread Keith
Hahaha! Actually this is my first time coding, so I still adapting the 
correct way to do it.
FYI, for the sake of minimizing the coding and viewing space occupied of the 
code, I even replace the switch block with array if the cases are just 
merely selecting pool of variables/categories/parameters!

I'm still learning the best practice for coding. :-)

""Daevid Vincent""  wrote in message 
news:fef58898dc3544e3872458cd64661...@mascorp.com...

Whoa! I didn't even know you could use a switch statement like that. In 20
years of coding, I've never ever used a switch like that first "if/else"
style. PHP never ceases to amaze me in it's flexibility (and ability to
shoot yourself in the foot ;-p )

And remember, all your base are belong to Adam.


-Original Message-
From: Adam Randall [mailto:randa...@gmail.com]
Sent: Thursday, August 20, 2009 11:20 PM
To: Keith
Cc: php-general@lists.php.net
Subject: Re: [PHP] Is there limitation for switch case:
argument's value?

I've never understood why people use switch statements like this as in
reality you are using it more like an if/else block, but anyway.

The reason why your code is not working is that you are passing into
the switch the value of 0, or false, which means that when any of
those $sum == N conditionals comes up as false (say $sum == 8 ), then
that is what is returned because they match up. In PHP's eyes, 0 ==
false in switch statements.

To fix your code, change the switch( $sum ) to switch( true ):

switch( true )
{
case ($sum == 8):
echo "sum=8\n";
break;
case ($sum == 7 || $sum == 6):
echo "sum=7 or 6\n";
break;
case ($sum == 2 || $sum == 1):
echo "sum=2 or 1\n";
break;
case ($sum == 0):
echo "sum=0\n";
break;
default:
echo "sum=3/4/5\n";
break;
}

Or, write your switch like this:

switch( $sum )
{
case 8:
echo "sum=8\n";
break;
case 6:
case 7:
echo "sum=7 or 6\n";
break;
case 1:
case 2:
echo "sum=2 or 1\n";
break;
case 0:
echo "sum=0\n";
break;
default:
echo "sum=3/4/5\n";
break;
}

Regards,

Adam.

On Thu, Aug 20, 2009 at 8:40 PM,
Keith wrote:
> Hi,
> I encounter a funny limitation here with switch case as below:
> The value for $sum is worked as expected for 1 to 8, but not for 0.
> When the $sum=0, the first case will be return, which is "sum=8".
> Is there any limitation / rules for switch case? Thanks for advice!
>
> Keith
>
> $sum=0;
> switch($sum)
> {
>   case ($sum==8):
>   echo "sum=8";
>   break;
>   case ($sum==7 || $sum==6):
>   echo "sum=7 or 6";
>   break;
>   case ($sum==2 || $sum==1):
>   echo "sum=2 or 1";
>   break;
>   case 0:
>   echo "sum=0";
>   break;
>   default:
>   echo "sum=3/4/5";
>   break;
> }
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
Adam Randall
http://www.xaren.net
AIM: blitz574

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





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



Re: [PHP] Is there limitation for switch case: argument's value?

2009-08-22 Thread Lars Torben Wilson
2009/8/22 Keith :
> Thanks! Torben.
> I got the point now and it works! :-)
> I'm doing this because the statements of each cases is quite long, and I
> wish to have minimum coding without repetition.

Hi Keith,

Glad it works! I'm not sure how inverting the case statement helps you
minimize the code in each case. As both I and Adam showed, you can do
the same thing more efficiently (and IMHO much more readably) like
this:

switch ($sum)
{
case 8:

   break;
case 7:
case 6:
   break;
case 2:
case 1:
   break;
case 0:
   break;
default:
   break;
}

> "Lars Torben Wilson"  wrote in message
> news:36d4833b0908202323p3c858b5fn6a1d6775aa7f8...@mail.gmail.com...
>>
>> 2009/8/20 Keith :
>>>
>>> Hi,
>>> I encounter a funny limitation here with switch case as below:
>>> The value for $sum is worked as expected for 1 to 8, but not for 0.
>>> When the $sum=0, the first case will be return, which is "sum=8".
>>> Is there any limitation / rules for switch case? Thanks for advice!
>>>
>>> Keith
>>
>> Hi Keith,
>>
>> Try replacing 'switch($sum)' with 'switch(true)'.
>>
>> Note that unless you have very good reasons for using a switch
>> statement like this, and know exactly why you're doing it, it's often
>> better just to use it in the normal fashion. i.e.:
>>
>>      switch ($sum)
>>       {
>>       case 8:
>>           break;
>>       case 7:
>>       case 6:
>>           break;
>>       case 2:
>>       case 1:
>>           break;
>>       case 0:
>>           break;
>> default:
>>           break;
>>       }
>>
>> Some people like the syntax you've presented but honestly, there's
>> usually a better way to do it.
>>
>> This is also somewhat faster too, although you may only notice the
>> difference in very tight loops where you're counting every nanosecond.
>>
>>
>> Regards,
>>
>> Torben
>>
>>> $sum=0;
>>> switch($sum)
>>> {
>>>  case ($sum==8):
>>>  echo "sum=8";
>>>  break;
>>>      case ($sum==7 || $sum==6):
>>>      echo "sum=7 or 6";
>>>      break;
>>>  case ($sum==2 || $sum==1):
>>>  echo "sum=2 or 1";
>>>  break;
>>>      case 0:
>>>      echo "sum=0";
>>>      break;
>>>  default:
>>>  echo "sum=3/4/5";
>>>  break;
>>> }
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

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



Re: [PHP] Is there limitation for switch case: argument's value?

2009-08-22 Thread Lars Torben Wilson
Aargh. Slipped on the trigger there--premature Send. See below for
what I meant to send:

2009/8/22 Lars Torben Wilson :
> 2009/8/22 Keith :
>> Thanks! Torben.
>> I got the point now and it works! :-)
>> I'm doing this because the statements of each cases is quite long, and I
>> wish to have minimum coding without repetition.

Hi Keith,

Glad it works! I'm not sure how inverting the case statement helps you
minimize the code in each case. As both I and Adam showed, you can do
the same thing more efficiently (and IMHO more readably) like
this:

switch ($sum)
{
case 8:
echo "The sum is 8";
break;
case 7:
case 6:
echo "The sum is 7 or 6";
break;
case 2:
case 1:
echo "The sum is 2 or 1";
break;
case 0:
echo "The sum is 0";
break;
default:
echo "The sum is 3, 4, or 5";
break;
}

And if the code is getting so long that it's getting unwieldy, you
might want to look into breaking it out into separate functions.

Anyway, it's not a huge thing; personally though I feel that it's a
syntax that if ever used, should only be used for very good reasons.
Most of the uses I've seen of it, however, fall into the "overly
clever" category and add nothing in the end to the quality of the
code. That's totally a judgement call on my part, of course. :)

Again, glad it works, and keep on coding!


Regards,

Torben

>> "Lars Torben Wilson"  wrote in message
>> news:36d4833b0908202323p3c858b5fn6a1d6775aa7f8...@mail.gmail.com...
>>>
>>> 2009/8/20 Keith :

 Hi,
 I encounter a funny limitation here with switch case as below:
 The value for $sum is worked as expected for 1 to 8, but not for 0.
 When the $sum=0, the first case will be return, which is "sum=8".
 Is there any limitation / rules for switch case? Thanks for advice!

 Keith
>>>
>>> Hi Keith,
>>>
>>> Try replacing 'switch($sum)' with 'switch(true)'.
>>>
>>> Note that unless you have very good reasons for using a switch
>>> statement like this, and know exactly why you're doing it, it's often
>>> better just to use it in the normal fashion. i.e.:
>>>
>>>      switch ($sum)
>>>       {
>>>       case 8:
>>>           break;
>>>       case 7:
>>>       case 6:
>>>           break;
>>>       case 2:
>>>       case 1:
>>>           break;
>>>       case 0:
>>>           break;
>>> default:
>>>           break;
>>>       }
>>>
>>> Some people like the syntax you've presented but honestly, there's
>>> usually a better way to do it.
>>>
>>> This is also somewhat faster too, although you may only notice the
>>> difference in very tight loops where you're counting every nanosecond.
>>>
>>>
>>> Regards,
>>>
>>> Torben
>>>
 $sum=0;
 switch($sum)
 {
  case ($sum==8):
  echo "sum=8";
  break;
      case ($sum==7 || $sum==6):
      echo "sum=7 or 6";
      break;
  case ($sum==2 || $sum==1):
  echo "sum=2 or 1";
  break;
      case 0:
      echo "sum=0";
      break;
  default:
  echo "sum=3/4/5";
  break;
 }
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


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

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



Re: [PHP] daemon without pcntl_fork

2009-08-22 Thread Jim Lucas

Lars Torben Wilson wrote:

2009/8/20 Jim Lucas :

Lars Torben Wilson wrote:

2009/8/19 Per Jessen :

Jim Lucas wrote:

[snip]


I probably wouldn't have chosen PHP for the first one, but there's no
reason it shouldn't work.  For the second one, did you mean to
write "serial port"?  That's a bit of a different animal, I'm not sure
how far you'll get with php.


Here is what I have come up with so far.  Looks to satisfying my
needs:

tms_daemon

[snip]

# END OF SCRIPT

Looks good to me. It'll certainly do the job.


/Per

I agree with Per on all points--I probably wouldn't choose PHP as a
first choice for the first task, but the startup script you have shown
looks like a bog-standard startup script and should serve you well. I
haven't really gone over it with a fine-toothed comb though. Typos
etc. are still up to to you. :) Of course, the whole thing depends on
how tms_daemon behaves, but the startup script looks OK.

Can you explain in a bit more detail exactly what the second part (the
serial-network data logger) needs to do? I've written similar daemons
in C but not PHP--when I've needed to get something like that going
with a PHP script, I've used ser2net (which I linked to in an earlier
post) and been quite happy. Maybe you don't even have to do the hard
work yourself (or buy extra hardware to do it for you).


Cheers,

Torben


As for the second project, I asked about it in the previous thread about
the SMDR/CDR processor.

http://www.nabble.com/SMDR-CDR-daemon-processor-td25014822.html


Sorry, missed that thread.


Basically, I need to have a process that collects data via serial
(USB|RS323), connects to a remote port, or listens and receives data
from the PBX pushing it.

The most common is the RS232 serial connection.  Thats why I need to be
able to listen on the local RS232 port for data coming from the PBX system.

What I have built so far can connect to a remote machine, via a TCP/IP
connection, and wait for data to be push out the specified TCP port.

I haven't done it yet, but I know that I can easily build, using a
different project as a base, the version that would connect to a local
TCP/IP IP:PORT and wait for a PBX to send the data to that IP:PORT.

After the data has been received, the process flow will be the same with
all three methods.  Parse it, sanitize it, store it.

Hopefully that explains a little more.

Jim


In short, are you looking for a program which listens on a serial port
and a network port at the same time, and places any data received from
either into a database?



No, I am looking to build and application that will have the option doing either of the three 
methods.  All at separate times.


It will be configured to work one phone system at a time.  It will either listen locally, connect 
remotely via TCP/IP, or open a COM port.  But, at this point, I don't see a need to have the ability 
to have it doing more then one method at a time.


Thanks



Regards,

Torben



--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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