[PHP] Dumb Question - Casting
Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad rusty. Can someone explain why the second expression in this code snippet evaluates to 7 and not 8? $a = (int) (0.1 +0.7); echo $a\n; $x = (int) ((0.1 + 0.7) * 10); echo $x\n; $y = (int) (8); echo $y\n;
Re: [PHP] Dumb Question - Casting
On Thu, 2010-02-18 at 09:47 -0600, Chuck wrote: Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad rusty. Can someone explain why the second expression in this code snippet evaluates to 7 and not 8? $a = (int) (0.1 +0.7); echo $a\n; $x = (int) ((0.1 + 0.7) * 10); echo $x\n; $y = (int) (8); echo $y\n; It works as expected if you take out the int() parts in each line. I'm not sure why, but the use of int() seems to be screwing around with the results. That's why the first line outputs a 0. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Dumb Question - Casting
On Thu, Feb 18, 2010 at 10:50 AM, Ashley Sheridan a...@ashleysheridan.co.uk wrote: On Thu, 2010-02-18 at 09:47 -0600, Chuck wrote: Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad rusty. Can someone explain why the second expression in this code snippet evaluates to 7 and not 8? $a = (int) (0.1 +0.7); echo $a\n; $x = (int) ((0.1 + 0.7) * 10); echo $x\n; $y = (int) (8); echo $y\n; It works as expected if you take out the int() parts in each line. I'm not sure why, but the use of int() seems to be screwing around with the results. That's why the first line outputs a 0. Thanks, Ash http://www.ashleysheridan.co.uk Another fine example of floating point math. ?php $x = ((0.1 + 0.7) * 10); echo ((0.1 + 0.7) * 10) === $x\n; // ((0.1 + 0.7) * 10) === 8 var_dump(8 == $x); // bool(false) var_dump($x - (int) $x); // float(1) var_dump(8 - $x); // float(8.8817841970013E-16) ? Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dumb Question - Casting
On Thu, Feb 18, 2010 at 16:47, Chuck chuck.car...@gmail.com wrote: Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad rusty. Can someone explain why the second expression in this code snippet evaluates to 7 and not 8? $a = (int) (0.1 +0.7); echo $a\n; $x = (int) ((0.1 + 0.7) * 10); echo $x\n; $y = (int) (8); echo $y\n; The reason why you get 7 instead of 8 is because you are using floating point arithmetic. 0.1 (i.e. the fraction 1/10) does not have a finite representation in base 2 (like you cannot finitely represent 1/3 in base 10). So the number 0.1 is represented in the computer as a number that is strictly less than 0.1 so when you do 0.1+0.7=x then you have x0.8 in the computer (think 7.999...). When you cast to int you just truncate the number, i.e. you chop off the fractional part leaving you with 7. -- Daniel Egeberg -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dumb Question - Casting
According to the PHP manual using the same expression, Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results. My guess is that since it is an expression of floating points, that the result is not quite 8 (for whatever reason). Therefore, it is rounded towards 0. Of course, that is only a guess, and I have no true documentation on it. Ashley Sheridan wrote: On Thu, 2010-02-18 at 09:47 -0600, Chuck wrote: Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad rusty. Can someone explain why the second expression in this code snippet evaluates to 7 and not 8? $a = (int) (0.1 +0.7); echo $a\n; $x = (int) ((0.1 + 0.7) * 10); echo $x\n; $y = (int) (8); echo $y\n; It works as expected if you take out the int() parts in each line. I'm not sure why, but the use of int() seems to be screwing around with the results. That's why the first line outputs a 0. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Dumb Question - Casting
Daniel Egeberg wrote: On Thu, Feb 18, 2010 at 16:47, Chuck chuck.car...@gmail.com wrote: Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad rusty. Can someone explain why the second expression in this code snippet evaluates to 7 and not 8? $a = (int) (0.1 +0.7); echo $a\n; $x = (int) ((0.1 + 0.7) * 10); echo $x\n; $y = (int) (8); echo $y\n; The reason why you get 7 instead of 8 is because you are using floating point arithmetic. 0.1 (i.e. the fraction 1/10) does not have a finite representation in base 2 (like you cannot finitely represent 1/3 in base 10). So the number 0.1 is represented in the computer as a number that is strictly less than 0.1 so when you do 0.1+0.7=x then you have x0.8 in the computer (think 7.999...). When you cast to int you just truncate the number, i.e. you chop off the fractional part leaving you with 7. yup as Daniel pointed out; this is correct - love floats casting! see: $y = ((0.1 + 0.7) * 10); $x = (int)$y; $z = intval($y); var_dump($y); var_dump(serialize($y)); var_dump($x); var_dump($z); outputs: float(8) string(55) d:7.99911182158029987476766109466552734375; int(7) int(7) lovely! - note: serializing gives us the exact value of the float regards, Nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] DUMB QUESTION I'M SURE
I'm just starting out, this is my script... ? $name = $_POST['username']; $name = $name; $db = mysql_connect(localhost); mysql_select_db(vinyldealers,$db); $query = SELECT shops.name FROM shops WHERE name = .$name.; $result = mysql_query($query); while ($record = mysql_fetch_assoc($result)) { while (list($feildname, $feildvalue) = each ($record)) { echo $feildname. : B .$feildvalue. /BBR; } echoBR; } ? This is the error I get... Parse error: parse error, unexpected ':' in C:\Xitami\webpages\php\result1.php on line 12 I dunno what I'm doing wrong. Help please??? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] DUMB QUESTION I'M SURE
Hi, Friday, January 3, 2003, 4:18:05 PM, you wrote: AF I'm just starting out, this is my script... AF ? AF $name = $_POST['username']; AF $name = $name; AF $db = mysql_connect(localhost); AF mysql_select_db(vinyldealers,$db); AF $query = SELECT shops.name FROM shops WHERE name = .$name.; AF $result = mysql_query($query); AF while ($record = mysql_fetch_assoc($result)) { AF while (list($feildname, $feildvalue) = each ($record)) { AF echo $feildname. : B .$feildvalue. /BBR; AF } AF echoBR; AF } ? AF This is the error I get... AF Parse error: parse error, unexpected ':' in AF C:\Xitami\webpages\php\result1.php on line 12 AF I dunno what I'm doing wrong. Help please??? Don't need the . at the end of this line: $query = SELECT shops.name FROM shops WHERE name = .$name.; -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] DUMB QUESTION I'M SURE
Try by adding a backslash to the : there (...\: B...) Cesar L. Aracena [EMAIL PROTECTED] [EMAIL PROTECTED] (0299) 156-356688 Neuquén (8300) Capital Argentina -Mensaje original- De: Adam French [mailto:[EMAIL PROTECTED]] Enviado el: viernes, 03 de enero de 2003 3:18 Para: [EMAIL PROTECTED] Asunto: [PHP] DUMB QUESTION I'M SURE I'm just starting out, this is my script... ? $name = $_POST['username']; $name = $name; $db = mysql_connect(localhost); mysql_select_db(vinyldealers,$db); $query = SELECT shops.name FROM shops WHERE name = .$name.; $result = mysql_query($query); while ($record = mysql_fetch_assoc($result)) { while (list($feildname, $feildvalue) = each ($record)) { echo $feildname. : B .$feildvalue. /BBR; } echoBR; } ? This is the error I get... Parse error: parse error, unexpected ':' in C:\Xitami\webpages\php\result1.php on line 12 I dunno what I'm doing wrong. Help please??? -- 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] DUMB QUESTION I'M SURE
What they mean is to make that line: $query = SELECT shops.name FROM shops WHERE name = $name; Cesar L. Aracena [EMAIL PROTECTED] [EMAIL PROTECTED] (0299) 156-356688 Neuquén (8300) Capital Argentina -Mensaje original- De: Adam French [mailto:[EMAIL PROTECTED]] Enviado el: viernes, 03 de enero de 2003 3:18 Para: [EMAIL PROTECTED] Asunto: [PHP] DUMB QUESTION I'M SURE I'm just starting out, this is my script... ? $name = $_POST['username']; $name = $name; $db = mysql_connect(localhost); mysql_select_db(vinyldealers,$db); $query = SELECT shops.name FROM shops WHERE name = .$name.; $result = mysql_query($query); while ($record = mysql_fetch_assoc($result)) { while (list($feildname, $feildvalue) = each ($record)) { echo $feildname. : B .$feildvalue. /BBR; } echoBR; } ? This is the error I get... Parse error: parse error, unexpected ':' in C:\Xitami\webpages\php\result1.php on line 12 I dunno what I'm doing wrong. Help please??? -- 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] DUMB QUESTION I'M SURE
I think it's just the $query var not having the right number of quotes. I added single quotes around the value. ? $name = $_POST['username']; $name = $name; $db = mysql_connect(localhost); mysql_select_db(vinyldealers,$db); $query = SELECT shops.name FROM shops WHERE name = '.$name.'; $result = mysql_query($query); while ($record = mysql_fetch_assoc($result)) { while (list($feildname, $feildvalue) = each ($record)) { echo $feildname. : B .$feildvalue. /BBR; } echoBR; } ? HTH, Jason k Larson Adam French wrote: I'm just starting out, this is my script... ? $name = $_POST['username']; $name = $name; $db = mysql_connect(localhost); mysql_select_db(vinyldealers,$db); $query = SELECT shops.name FROM shops WHERE name = .$name.; $result = mysql_query($query); while ($record = mysql_fetch_assoc($result)) { while (list($feildname, $feildvalue) = each ($record)) { echo $feildname. : B .$feildvalue. /BBR; } echoBR; } ? This is the error I get... Parse error: parse error, unexpected ':' in C:\Xitami\webpages\php\result1.php on line 12 I dunno what I'm doing wrong. Help please??? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] DUMB QUESTION I'M SURE
Quoting Cesar Aracena [EMAIL PROTECTED]: ### Try by adding a backslash to the : there (...\: B...) ### ### Cesar L. Aracena ### [EMAIL PROTECTED] ### [EMAIL PROTECTED] ### (0299) 156-356688 ### Neuquén (8300) Capital ### Argentina ### ### ### -Mensaje original- ### De: Adam French [mailto:[EMAIL PROTECTED]] ### Enviado el: viernes, 03 de enero de 2003 3:18 ### Para: [EMAIL PROTECTED] ### Asunto: [PHP] DUMB QUESTION I'M SURE ### ### I'm just starting out, this is my script... ### ### ? ### $name = $_POST['username']; ### $name = $name; ### $db = mysql_connect(localhost); ### mysql_select_db(vinyldealers,$db); ### $query = SELECT shops.name FROM shops WHERE name = .$name.; you might also want to format the above like this: $query = SELECT shops.name FROM shops WHERE name = $name; you're gonna get an error in your sql syntax otherwise ### $result = mysql_query($query); ### while ($record = mysql_fetch_assoc($result)) { ### while (list($feildname, $feildvalue) = each ($record)) { ### echo $feildname. : B .$feildvalue. /BBR; ### } ### echoBR; ### } ### ? ### ### This is the error I get... ### ### Parse error: parse error, unexpected ':' in ### C:\Xitami\webpages\php\result1.php on line 12 ### ### I dunno what I'm doing wrong. Help please??? ### ### ### ### -- ### 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
[PHP] Dumb Question
And I feel foolish asking... What is meant by 'procedural code' ??? -- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dumb Question
My "dumb" answer :) Try Google. Type: "procedural code" You might want to check, "object-oriented" as well... I'm sure, you'll find helpful explanations... - E And I feel foolish asking... What is meant by 'procedural code' ??? -- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ $B2q0wEPO?$OL5NA!&=<
Re: [PHP] Dumb Question
Google didn't have much to offer. But if I should also check 'object-oriented' then I believe it deals with classes. I thought it was something else. Just trying to figure out if phpdoc is for me, which it seems like its not :( Thanks @ Edwin wrote: My "dumb" answer :) Try Google. Type: "procedural code" You might want to check, "object-oriented" as well... I'm sure, you'll find helpful explanations... - E And I feel foolish asking... What is meant by 'procedural code' ??? -- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ $B2q0wEPO?$OL5NA!&=<
Re: [PHP] Dumb Question
Google didn't have much to offer. Sorry 'bout that. Actually, if you have an idea of what OO ("object-oriented") is, I think I can say that "procedural" is just the opposite of it. I tried Google myself and this came out on top: "Writing Procedural Code in Non-Procedural SQL" There's a short explanation but I think it's enough to give you some hint about "procedural code". - E But if I should also check 'object-oriented' then I believe it deals with classes. I thought it was something else. Just trying to figure out if phpdoc is for me, which it seems like its not :( Thanks :) @ Edwin wrote: My "dumb" answer :) Try Google. Type: "procedural code" You might want to check, "object-oriented" as well... I'm sure, you'll find helpful explanations... - E And I feel foolish asking... What is meant by 'procedural code' ??? -- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ $B2q0wEPO?$OL5NA!&=<
Re: [PHP] Dumb Question
On Sat, 31 Aug 2002 14:04:11 -0400, you wrote: And I feel foolish asking... What is meant by 'procedural code' ??? It's the opposite of declarative code. Here's a page that briefly explains the difference: http://www.wdvl.com/Authoring/DB/SQL/BeginSQL/beginSQL2_1.html and http://www.wdvl.com/Authoring/DB/SQL/BeginSQL/beginSQL2_2.html There may be other contexts that the term procedural could be used in, and if so it may have other meanings that I am not aware of -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dumb Question
Here is my stab at it. One person described it as the opposite of OO. So something similar to - ?php do_this() { // do this code } do_that() { // do that code } if (isset( $_GET['foo'] )) { do_this(); } else { do_that(); } ? would be considered procedural code. If Im wrong I stand corrected Michael Sims wrote: On Sat, 31 Aug 2002 14:04:11 -0400, you wrote: And I feel foolish asking... What is meant by 'procedural code' ??? It's the opposite of declarative code. Here's a page that briefly explains the difference: http://www.wdvl.com/Authoring/DB/SQL/BeginSQL/beginSQL2_1.html and http://www.wdvl.com/Authoring/DB/SQL/BeginSQL/beginSQL2_2.html There may be other contexts that the term procedural could be used in, and if so it may have other meanings that I am not aware of -- Gerard Samuel http://www.trini0.org:81/ http://dev.trini0.org:81/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Dumb question
Hey guys, got a basic question for you... ? function getvar() { $vari = bollocks; } getvar(); echo $vari; ? How do I make that function return the variable? Thanks, Liam -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dumb question
You need to declare $vari as a global variable. eg; ? function getvar() { global $vari; $vari = bollocks; } getvar(); echo $vari; ? Best Regards Bob Irwin Server Admin Web Programmer Planet Netcom - Original Message - From: Liam MacKenzie [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, August 22, 2002 1:27 PM Subject: [PHP] Dumb question Hey guys, got a basic question for you... ? function getvar() { $vari = bollocks; } getvar(); echo $vari; ? How do I make that function return the variable? Thanks, Liam -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Scanned by PeNiCillin http://safe-t-net.pnc.com.au/ Scanned by PeNiCillin http://safe-t-net.pnc.com.au/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[Fwd: Re: [PHP] Dumb question]
---BeginMessage--- ? function getvar() { $vari = bollocks; # this $vari is local to function } $vari = getvar(); # this $vari is in global space echo $vari ? On Wed, 2002-08-21 at 20:35, Bob Irwin wrote: You need to declare $vari as a global variable. eg; ? function getvar() { global $vari; $vari = bollocks; } getvar(); echo $vari; ? Best Regards Bob Irwin Server Admin Web Programmer Planet Netcom - Original Message - From: Liam MacKenzie [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, August 22, 2002 1:27 PM Subject: [PHP] Dumb question Hey guys, got a basic question for you... ? function getvar() { $vari = bollocks; } getvar(); echo $vari; ? How do I make that function return the variable? Thanks, Liam -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Scanned by PeNiCillin http://safe-t-net.pnc.com.au/ Scanned by PeNiCillin http://safe-t-net.pnc.com.au/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ---End Message--- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Dumb question on terminology
Some arrays use the format: $row-data while some use: $row['data'] What is the terminology of these types so I sound like I know what I'm talking about. Also, when and how do each come about? Dean
RE: [PHP] Dumb question on terminology
In a nutshell: $row-data is referring to an object, not an array, that has an attribute of $data $row['data'] is an associative array - ie, indexes are non-numerical - with an index of data Martin -Original Message- From: Dean Householder [mailto:[EMAIL PROTECTED]] Sent: Wednesday, February 27, 2002 11:32 AM To: [EMAIL PROTECTED] Subject: [PHP] Dumb question on terminology Some arrays use the format: $row-data while some use: $row['data'] What is the terminology of these types so I sound like I know what I'm talking about. Also, when and how do each come about? Dean