Thread: Can C++ Replace PHP?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Location
    Birmingham, UK.
    Posts
    14

    Can C++ Replace PHP?

    Might seem like a stupid question, but I have been using PHP for years. I was wondering, can C++ be used to replace that?

    I know it's more powerful, and would allow me to do ALOT more. Would it be faster, and more secure than PHP?

    I'm not at the stage to develop much with C++, still learning, but asking in advanced.

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    It will be faster/secure is according to the way you program.

    Btw, they are completely different tools for different reasons.

    PHP is for web, sometime for desktop (PHP-GTK).

    C++ is for OS, drivers, desktop apps, server apps, etc.
    Just GET it OFF out my mind!!

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by audinue View Post
    It will be faster/secure is according to the way you program.

    Btw, they are completely different tools for different reasons.

    PHP is for web, sometime for desktop (PHP-GTK).

    C++ is for OS, drivers, desktop apps, server apps, etc.
    Not so. A C++ program has just as much (if not more) capability as a server extension. The only thing is you'd have to design a system that can process the server's interaction protocol (which could be a reusable library, of course), and the program would basically have full access to the CPU, which may not be suitable for some environments (eg: the server is owned by someone else).

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    can C++ be used to replace that?
    It can, but that doesn't necesarily mean it should. They're suited for different things, so what exactly are you wanting to do? Incidentally, when people say C++ is more powerful, that's a double-edged sword. It also makes mistakes more costly. As audinue said - if you're not careful it could make you even more vulnerable.

    edit: I just realized I only said negative things in my post. C++ is a more respected language, for many valid reasons - so learning it would certainly not be a waste. It gives you a great deal of flexibility and potential gains. But as I said, it really depends on what you want to do - they both have ups and downs.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    From a pure technical perspective, it would always be possible to use C++ as a replacement of PHP. The reverse is also technically true.

    From memory, a number of the PHP support binaries (parsers, optimisers) are written in C. If something is written in C, it is technically also C++ - albeit non-idiomatic C++, although it could be rewritten in idiomatic C++ if needed. [By "non-idiomatic C++" I refer to code that can be compiled with a C++ compiler, but does not use tend to use techniques that are considered "good practice" among leading C++ experts and professionals. "Idiomatic C++" is written mostly using "good practice"].

    Practically, it's all about trade-offs. The fact that, technically, PHP and C++ could be used to replace each other does not mean they should be. Each is designed with a particular set of programming tasks in mind. There are some things a PHP programmer can achieve much more easily than a C++ programmer, and vice versa. PHP is a language optimised for server side scripting and, to a lesser extent, supports command line scripting. PHP programmers can also use PHP to write desktop applications: although such programming is in the realm of "advanced" techniques. C++ is a language designed for general-purpose programming: being a general-purpose language it can be used to write server-side applications and command line programs, and also - with some reliance on third-party libraries - desktop applications.

    So, while C++ could replace PHP, it is not necessarily a good idea to do so. In some cases, you could find yourself rewriting simple PHP scripts into large and relatively complex C++ programs. That is not particularly productive. Conversely, if you sought to replace C++ with PHP, you could find yourself rewriting simple C++ programs into significantly more difficult PHP scripts. This reflects the fact there are some things each language facilitates more easily than the other.

    So the practical solution - at least for people who have skills in both languages - is to use both. Decide which language you use based on the task at hand, and understanding of both languages. In some cases, it might be easier to write a system using both languages (eg some functionality in PHP, some other functionality in C++) although there is also the effort to interface between the parts that needs to be taken into account.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    ... Or following an established analogy, you can hammer a nail down with a screwdriver. Problem is that it's much more difficult.

    On another note though I do have a question: The idea PHP could also be used to replace C++ is news to me. Could PHP build, say, a C++ compiler, or didn't you need to go back to C or C++ to produce the relevant libraries for the PHP interpreter?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Think C++ like iron which is general enough to make specialized things, for example knife (PHP), can (Apache), ring (Notepad), etc.

    To refine the iron into or doing like what knife do, you'll need something more too for sure.
    Just GET it OFF out my mind!!

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by sean View Post
    Incidentally, when people say C++ is more powerful, that's a double-edged sword. It also makes mistakes more costly. As audinue said - if you're not careful it could make you even more vulnerable.
    C makes t easy to shoot yourself in the foot. C++ makes it harder, but when you do it blows off your leg. -bjarne stroustrup

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yes, however it comes at a cost. Re-implementing existing PHP functions may not always be easy. I've been developing a server-side application and although PHP or Python might be easier, I'm doing it in C++ for the power, and speed. Unfortunately, I run into speed bumps like this:

    Nabble - Regex

    I have yet to find a really good URL parser (hopefully not attached to an additional library, except boost). Oh well. Maybe Boost::Spirit. I dunno. Cool thing is if I get desperate, I can probably just load the PHP C API or source.
    Last edited by Dae; 08-16-2009 at 02:57 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Dae View Post
    Yes, however it comes at a cost. Re-implementing existing PHP functions may not always be easy. I've been developing a server-side application and although PHP or Python might be easier, I'm doing it in C++ for the power, and speed. Unfortunately, I run into speed bumps like this:

    Nabble - Regex

    I have yet to find a really good URL parser (hopefully not attached to an additional library, except boost). Oh well. Maybe Boost::Spirit. I dunno. Cool thing is if I get desperate, I can probably just load the PHP C API or source.
    A URL parser is quite easy to implement. They follow quite specific guidelines. I don't know the exact rules, like which characters are allowed where, so if you'd want it a 100% standards compliant you would have to read the RFC. But if it can accept wrong URLs as well, it should be quite easy.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by EVOEx View Post
    A URL parser is quite easy to implement. They follow quite specific guidelines. I don't know the exact rules, like which characters are allowed where, so if you'd want it a 100% standards compliant you would have to read the RFC. But if it can accept wrong URLs as well, it should be quite easy.
    Yep. Trivial.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mario F. View Post
    .
    On another note though I do have a question: The idea PHP could also be used to replace C++ is news to me. Could PHP build, say, a C++ compiler, or didn't you need to go back to C or C++ to produce the relevant libraries for the PHP interpreter?
    Using PHP to build a C++ compiler would be a fairly difficult task but it would be technically possible - albeit with a lot of effort, and the resultant compiler would probably have less performance than a compiler written in another language.

    Look up concepts like Turing completeness.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by EVOEx View Post
    A URL parser is quite easy to implement. They follow quite specific guidelines. I don't know the exact rules, like which characters are allowed where, so if you'd want it a 100% standards compliant you would have to read the RFC. But if it can accept wrong URLs as well, it should be quite easy.
    Quote Originally Posted by Sebastiani View Post
    Yep. Trivial.
    I can parse URL's fine. The specific guideline's required to parse URL's 100% correctly requires that regular expression (others will result in incorrect parsing sooner or later), and that regular expression does not work with Boost::Regex (at least without some obscure attribute I have to try and find). Hence what I stated. So your point is?

    My point is, you are basically re-building a library, that probably isn't as accurate (without putting some extra time into it).

    PHP's URL parser:
    Code:
    PHPAPI php_url *php_url_parse_ex(char const *str, int length)
    {
    	char port_buf[6];
    	php_url *ret = ecalloc(1, sizeof(php_url));
    	char const *s, *e, *p, *pp, *ue;
    		
    	s = str;
    	ue = s + length;
    
    	/* parse scheme */
    	if ((e = memchr(s, ':', length)) && (e - s)) {
    		/* validate scheme */
    		p = s;
    		while (p < e) {
    			/* scheme = 1*[ lowalpha | digit | "+" | "-" | "." ] */
    			if (!isalpha(*p) && !isdigit(*p) && *p != '+' && *p != '.' && *p != '-') {
    				if (e + 1 < ue) {
    					goto parse_port;
    				} else {
    					goto just_path;
    				}
    			}
    			p++;
    		}
    	
    		if (*(e + 1) == '\0') { /* only scheme is available */
    			ret->scheme = estrndup(s, (e - s));
    			php_replace_controlchars_ex(ret->scheme, (e - s));
    			goto end;
    		}
    
    		/* 
    		 * certain schemas like mailto: and zlib: may not have any / after them
    		 * this check ensures we support those.
    		 */
    		if (*(e+1) != '/') {
    			/* check if the data we get is a port this allows us to 
    			 * correctly parse things like a.com:80
    			 */
    			p = e + 1;
    			while (isdigit(*p)) {
    				p++;
    			}
    			
    			if ((*p == '\0' || *p == '/') && (p - e) < 7) {
    				goto parse_port;
    			}
    			
    			ret->scheme = estrndup(s, (e-s));
    			php_replace_controlchars_ex(ret->scheme, (e - s));
    			
    			length -= ++e - s;
    			s = e;
    			goto just_path;
    		} else {
    			ret->scheme = estrndup(s, (e-s));
    			php_replace_controlchars_ex(ret->scheme, (e - s));
    		
    			if (*(e+2) == '/') {
    				s = e + 3;
    				if (!strncasecmp("file", ret->scheme, sizeof("file"))) {
    					if (*(e + 3) == '/') {
    						/* support windows drive letters as in:
    						   file:///c:/somedir/file.txt
    						*/
    						if (*(e + 5) == ':') {
    							s = e + 4;
    						}
    						goto nohost;
    					}
    				}
    			} else {
    				if (!strncasecmp("file", ret->scheme, sizeof("file"))) {
    					s = e + 1;
    					goto nohost;
    				} else {
    					length -= ++e - s;
    					s = e;
    					goto just_path;
    				}	
    			}
    		}	
    	} else if (e) { /* no scheme, look for port */
    		parse_port:
    		p = e + 1;
    		pp = p;
    		
    		while (pp-p < 6 && isdigit(*pp)) {
    			pp++;
    		}
    		
    		if (pp-p < 6 && (*pp == '/' || *pp == '\0')) {
    			memcpy(port_buf, p, (pp-p));
    			port_buf[pp-p] = '\0';
    			ret->port = atoi(port_buf);
    		} else {
    			goto just_path;
    		}
    	} else {
    		just_path:
    		ue = s + length;
    		goto nohost;
    	}
    	
    	e = ue;
    	
    	if (!(p = memchr(s, '/', (ue - s)))) {
    		if ((p = memchr(s, '?', (ue - s)))) {
    			e = p;
    		} else if ((p = memchr(s, '#', (ue - s)))) {
    			e = p;
    		}
    	} else {
    		e = p;
    	}	
    		
    	/* check for login and password */
    	if ((p = zend_memrchr(s, '@', (e-s)))) {
    		if ((pp = memchr(s, ':', (p-s)))) {
    			if ((pp-s) > 0) {
    				ret->user = estrndup(s, (pp-s));
    				php_replace_controlchars_ex(ret->user, (pp - s));
    			}	
    		
    			pp++;
    			if (p-pp > 0) {
    				ret->pass = estrndup(pp, (p-pp));
    				php_replace_controlchars_ex(ret->pass, (p-pp));
    			}	
    		} else {
    			ret->user = estrndup(s, (p-s));
    			php_replace_controlchars_ex(ret->user, (p-s));
    		}
    		
    		s = p + 1;
    	}
    
    	/* check for port */
    	if (*s == '[' && *(e-1) == ']') {
    		/* Short circuit portscan, 
    		   we're dealing with an 
    		   IPv6 embedded address */
    		p = s;
    	} else {
    		/* memrchr is a GNU specific extension
    		   Emulate for wide compatability */
    		for(p = e; *p != ':' && p >= s; p--);
    	}
    
    	if (p >= s && *p == ':') {
    		if (!ret->port) {
    			p++;
    			if (e-p > 5) { /* port cannot be longer then 5 characters */
    				STR_FREE(ret->scheme);
    				STR_FREE(ret->user);
    				STR_FREE(ret->pass);
    				efree(ret);
    				return NULL;
    			} else if (e - p > 0) {
    				memcpy(port_buf, p, (e-p));
    				port_buf[e-p] = '\0';
    				ret->port = atoi(port_buf);
    			}
    			p--;
    		}	
    	} else {
    		p = e;
    	}
    	
    	/* check if we have a valid host, if we don't reject the string as url */
    	if ((p-s) < 1) {
    		STR_FREE(ret->scheme);
    		STR_FREE(ret->user);
    		STR_FREE(ret->pass);
    		efree(ret);
    		return NULL;
    	}
    
    	ret->host = estrndup(s, (p-s));
    	php_replace_controlchars_ex(ret->host, (p - s));
    	
    	if (e == ue) {
    		return ret;
    	}
    	
    	s = e;
    	
    	nohost:
    	
    	if ((p = memchr(s, '?', (ue - s)))) {
    		pp = strchr(s, '#');
    		
    		if (pp && pp < p) {
    			p = pp;
    			pp = strchr(pp+2, '#');
    		}
    	
    		if (p - s) {
    			ret->path = estrndup(s, (p-s));
    			php_replace_controlchars_ex(ret->path, (p - s));
    		}	
    	
    		if (pp) {
    			if (pp - ++p) { 
    				ret->query = estrndup(p, (pp-p));
    				php_replace_controlchars_ex(ret->query, (pp - p));
    			}
    			p = pp;
    			goto label_parse;
    		} else if (++p - ue) {
    			ret->query = estrndup(p, (ue-p));
    			php_replace_controlchars_ex(ret->query, (ue - p));
    		}
    	} else if ((p = memchr(s, '#', (ue - s)))) {
    		if (p - s) {
    			ret->path = estrndup(s, (p-s));
    			php_replace_controlchars_ex(ret->path, (p - s));
    		}	
    		
    		label_parse:
    		p++;
    		
    		if (ue - p) {
    			ret->fragment = estrndup(p, (ue-p));
    			php_replace_controlchars_ex(ret->fragment, (ue - p));
    		}	
    	} else {
    		ret->path = estrndup(s, (ue-s));
    		php_replace_controlchars_ex(ret->path, (ue - s));
    	}
    end:
    	return ret;
    }
    Last edited by Dae; 08-16-2009 at 06:04 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I can parse URL's fine. The specific guideline's required to parse URL's 100% correctly requires that regular expression (others will result in incorrect parsing sooner or later), and that regular expression does not work with Boost::Regex (at least without some obscure attribute I have to try and find). Hence what I stated. So your point is?

    My point is, you are basically re-building a library, that probably isn't as accurate (without putting some extra time into it).
    Regular expressions are just tools - parsing a URL could be done with or without them, the only requirement is that the implementation is complete and correct. Personally I'd use my spirit-like parser template library, but even without it the job wouldn't be too difficult (the PHP implementation looks more complicated than necessary, IMO). It just boils down to what you're willing to do, I guess.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm not sure either what you are trying to say, Dae.

    It comes with the territory. Implementing an existing solution in another programming language automatically entails some sort of analysis. But your problem isn't an issue with C++ and PHP. Just regular expressions. Apparently you got to your C++ abstraction rather quickly and without much effort.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PHP installation
    By ssharish2005 in forum Tech Board
    Replies: 8
    Last Post: 11-23-2007, 09:42 PM
  2. Replace a list with a new entry?
    By niponki in forum C Programming
    Replies: 4
    Last Post: 08-17-2005, 10:41 AM
  3. PHP on my Computer!
    By xxxrugby in forum Tech Board
    Replies: 4
    Last Post: 03-15-2005, 09:34 AM
  4. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  5. Need a new way to replace a constant
    By RustGod in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2001, 03:05 PM