TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   quick question regarding c and php. (http://www.talkphp.com/general/3092-quick-question-regarding-c-php.html)

masfenix 07-10-2008 03:00 AM

quick question regarding c and php.
 
I know a bit of C and Kalle here (whos on the php team) has shown me some useful c source!

Just wondering since I am a .net developer (and I dont have PHP installed)
what would the following C code converted to PHP yield?

Code:


main()
{
        int x = 10;
        while(x) {
                x--;
                printf("X Value: %d\n", x);
        }

       

}


I've attempted to code it to make it easier for you guys to copy and paste:

Code:

<?PHP

$x = 10;
while ($x)
{
$x = $x - 1;
echo $x
}


?>

My question is will the PHP code be stuck in a infinite loop? I ask because C code parses true and false as 1 and 0, and therefore when that "x" value eaches zero, its assumed "false" and the while loop exits

delayedinsanity 07-10-2008 03:10 AM

Install PHP! It's actually fairly easy. Download Apache, download PHP, off you go.

PHP considers true and false 1 and 0 respectively as well, and the script stops at 0.
-m

Enfernikus 07-10-2008 04:40 AM

OR you can be lazy and install WampServer2 and I'm pretty sure php supports $x-- and --$x the latter being the faster.

sketchMedia 07-10-2008 09:31 AM

post or pre incrementing/decrementing depends on what you need the script to do, both can produce very different results, consider this:
PHP Code:

<?php

$a 
$b 1;

$c = ++$a;
$d $b++;

printf('$a: %d <br /> $b: %d <br /> $c: %d <br /> $d: %d',$a$b$c$d);

This script may not produce the results expected. In this script we set up our base vars ($a, $b) next we create $c and assign it the the value of ++$a, in other words 'Increment $a then put then assign $c the value'. Next we create $d and assign it the the value of $b++, in other words 'Assign $d the value of $b, then increment $b'. If we were to print this it would produce:
Code:

2
2
2
1

The speed difference comes because when you use post increment or decrement ($i++) PHP copies the value to a tmp variable, then it increments the value and returns the value which was stored before the incrementation, pre increment/decrement however does not, it just returns the value of the variable +/- 1

Anyway back to the topic, yes you are right C has no native boolean type (to my knowledge, prior to C99)
, instead you use 0 or 1 to flag true or false or you can use #define's or enum's:
Code:

#define TRUE  1
#define FALSE 0   
//or
typedef enum { FALSE, TRUE } boolean;

or user C99's stdbool:
c Code:
#include<stdbool>
bool boolean = true;
//etc
 
However php considers all these values as (Boolean)FALSE:
  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags
taken from: PHP: Booleans - Manual

masfenix 07-10-2008 02:48 PM

Thanks for the answers.

.NET is unable to do it because of strong types. It will not compile because "int can not be converted to boolean".

I just tried it with Java, and java does not seem to compile as well.

Awesome, by the way is Apache really neccessary? I have IIS 7 so I am just thinking to install PHP on that.

sketchMedia 07-10-2008 02:54 PM

Quote:

I just tried it with Java, and java does not seem to compile as well.
Java doesn't allow an integer expression where by false is (int)0 and true is any non-zero value, as boolean variables are represented by the primitive type 'boolean' and java doesn't allow casting to and from boolean i believe (my java is a bit sketchy).

Quote:

Awesome, by the way is Apache really neccessary? I have IIS 7 so I am just thinking to install PHP on that.
No not at all, you can install and use PHP in a variety of ways, including on a command line interface. I am not sure of how to install on IIS as i don't usually use windows that often, but I'm sure somebody on here will.

delayedinsanity 07-10-2008 04:50 PM

How to install PHP on IIS: BillS IIS Blog
How To Install IIS 7, PHP5 and MySQL 5.1 on Windows Server 2008 - Part 2

...seems to make it sound fairly simple.
-m


All times are GMT. The time now is 03:10 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0