TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-10-2008, 03:00 AM   #1 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 31
Thanks: 1
masfenix is on a distinguished road
Default 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
masfenix is offline  
Reply With Quote
Old 07-10-2008, 03:10 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
masfenix (07-10-2008)
Old 07-10-2008, 04:40 AM   #3 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

OR you can be lazy and install WampServer2 and I'm pretty sure php supports $x-- and --$x the latter being the faster.
Enfernikus is offline  
Reply With Quote
Old 07-10-2008, 09:31 AM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 07-10-2008 at 03:15 PM. Reason: error in explaination
sketchMedia is offline  
Reply With Quote
Old 07-10-2008, 02:48 PM   #5 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 31
Thanks: 1
masfenix is on a distinguished road
Default

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.
masfenix is offline  
Reply With Quote
Old 07-10-2008, 02:54 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 07-10-2008 at 03:02 PM. Reason: clarified a point that was a bit cloudy
sketchMedia is offline  
Reply With Quote
Old 07-10-2008, 04:50 PM   #7 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
sketchMedia (07-11-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:48 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design