I'm new to PHP and will like to develop a mobile app that interacts with the server (By putting and pulling datas from the server). Initially I was using Java, but finacial issues I decided to use PHP because getting domain that uses java is expensive.
My question is that does PHP controls multitasking ? reason been that since I will have thousands of users connected to my server probably the same. I llok forward for your answers Thanks
How should PHP have control over multitasking?
PHP interprets a PHP-Script to one point in time when a http-Request occurs on the Script.
PHP does not do multi-threading. It's a single-process-execution kind of scripting language.
However, when set up as a server-side language, it's usually paired up with a HTTP server like Apache, IIS or Nginx, who manage several child processes to handle multiple requests. - If you set it up like a normal server-side language, on top of one of those HTTP servers, you will have no problems handling a lot of parallel traffic.
Related
Let's say I have a web app server coded in PHP, would it be possible to develop the app's server further using a different language (say, Python)?
If so, how would I go about doing this?
Yes it's possible (and pretty common), but as for a "how do I do this?" that's far too broad to answer.
For an example, I built a game using PHP for the front end (using Laravel), and then behind the scenes it processes games using python (on top of pyramid) and a few other bits and pieces.
Most low-cost web hosts will only provide one language that youw an work with (most commonly PHP), but if you run your own server you can run whatever you want to on there.
I have been looking at the node.js application vs php, I found many comparisons comparing these two server technique. Mostly people suggest that the javascript V8 engine is much faster than php in terms of running speed of a single file calculation.
I worked on some javascript code for Node.js, now I have this idea I don't know if it is correct.
In my opinion, Node.js runs a javascript application and listen on a port. so this javascript application is an application that is running on server computer. Therefore, the application code is all copied in the memory of the computer. stuff like global variables are declared and saved at the beginning when node.js execute this program. So any new request come in, the server can use these variables very efficiently.
In php, however, the php program execute *.php file based on request. Therefore, if some request is for www.xx.com/index.php, then the program will execute index.php, and in which, there may be stuff like
require("globalVariables.php");
then, php.exe would go there and declare these variables again. same idea for functions and other objects...
So am I correct in thinking that php may not be a good idea when there are many other libraries that need to be included?
I have searched for the comparison, but nobody have talked about this.
Thanks
You are comparing different things. PHP depends on Apache or nginx (for example) to serve scripts, while Node.js is a complete server itself.
That's a big difference, cause when you load a php page, Apache will spawn a thread and run the script there. In Node all requests are served by the Node.js unique thread.
So, php and Node.js are different things, but regarding your concern: yes, you can mantain a global context in Node that will be loaded in memory all the time. On the other hand PHP loads, runs and exits all the time. But that's not the typical use case, Node.js web applications have templates, that have to be loaded and parsed, database calls, files... the real difference is the way Node.js handles heavy tasks: a single thread for javascript, an event queue, and external threads for filesystem, network and all that slow stuff. Traditional servers spawn threads for each connection, that's a very different approach.
How do PHP server pages handle multiple requests from different users?
I am used to C-like languages and they use multithreading. What does PHP use in this case?
The PHP interpreter is generally invoked by a webserver (like apache, lighthttpd, ...). The webserver then handles the requests (by threading, forking or whatever). Every instance of PHP is running sequentially, so there is no builtin multithreading.
There exists a PECL extension https://github.com/krakjoe/pthreads which adds multithreading to PHP.
That's really dependant upon the server you're using to handle these requests. PHP itself does not handle requests, but instead is used to parse the script and return the result. It is possible to write a web server in PHP but then again, this is all dependent on the server you are using.
It's actually a good question. Once several users try send requests to the same server , no confilct happens becuase the webserver creates an independent process, which you can call a session ( not a php session ) of it's own for each request.
PHP has nothing to do with this becuase it's only a script which is finally translated by the a machine .
I know the basics of both PHP and Node.js, but I don't understand why some people argue over which one is better... Can Node.js be used to render web pages like PHP can? For example, can you make a BBS using Node.js (I know you have access to DBs, but rwndering posts seems to be a problem)? It seems to me like Node.js can be used either as a very simple HTTP server, that only serves basic HTML, without changing it, or for communication (which I use it for). For example, I'm making a browser MMO game, and I use PHP to serve the site and the forum/devblog, and Node.js for the actual game. Or am I missing something?
Can Node.js be used to render web pages like PHP can?
Yes. There are some differences, but the main idea is to use Node.js on the server-side to serve responses to the requests, similarly to PHP. So again, yes, you can render web pages using Node.js.
Can you make a Bulletin Board System using Node.js?
Yes. Rendering posts also can be done within Node.js. You can render posts also on client side JavaScript, so why you think server-side JavaScript (Node.js) would be more limited? For templating see eg. {{ moustache }} or Pure.
The main advantage of Node.js over "standard" PHP is that JavaScript is event-driven and you use single thread for all the requests, whereas in PHP you use separate thread for every request.
Node.js lets you write your own "server" using only JavaScript. It simplifies a lot if you want to build eg. communication server.
More resources
For more comprehensive solution for rendering web pages in Node.js see eg. ExpressJS Node.js framework. You may specifically be interested in the way ExpressJS allows you to render views.
Someone will give a more detailed answer than me but basically node.js can replace both Apache and Php. It's a platform that allows you to create a web application using only javascript. It has a different model respect to apache because node is event driven (being written in javascript) while Apache uses threads.
Anyway take a look here for a decent tutorial on node, the best way to understand it's by using it i think
Node.js and PHP can both be used to build server-side applications, but they have different strengths and weaknesses. While Node.js excels at handling large amounts of I/O and real-time applications, PHP is often used for traditional web applications. They can be used together to complement each other's strengths.
This has been bugging me for awhile now.
In a deployed PHP web application one can upload a changed php script and have the updated file picked up by the web server without having to restart.
The problem? Ruby, Groovy, & Python, etc. are all "better" than PHP in terms of language expressiveness, concision, power, ...your-reason-here.
Currently, I am really enjoying Groovy (via Grails), but the reality is that the JVM does not do well (at all) with production dynamic reloading of application code. Basically, Permgen out of memory errors are a virtual guarantee, and that means application crash at anytime -- not good.
Ruby frameworks seem to have this solved somewhat from what I have read: Passenger has an option to dynamically reload changed files in polled directories on the next request (thus preventing connected users from being disconnected, session lost, etc.).
Standalone Python I am not sure about at all; it may, like PHP allow dynamic reloading of python scripts without web server restart.
As far as our web work is concerned, invariably clients wind up wanting to make changes to a deployed application regardless of how detailed and well planned the spec was. Telling the client, "sure, we'll implement that [simple] change at 4AM tomorrow [so as to not wreak havoc with connected users]", won't go over too well.
As of 2011 where are we at in terms of dynamic reloading and scripting languages? Are we forever doomed, relegated to the convenience of PHP, or the joys of non-PHP and being forced to restart a deployed application?
BTW, I am not at all a fan of JSPs, GSPs, and Ruby, Python templating equivalents, despite their reloadability. This is a cake & eat it too thread, where we can make a change to any aspect of the application and not have to restart.
You haven't specified a web server. If you're using Apache, mod_wsgi is your best bet for running Python web apps, and it has a reloading mechanism that doesn't require a server restart.
I think you're making a bigger deal out of this than it really is.
Any application for which it is that important that it never be down for 1/2 a minute (which is all it takes to reboot a server to pick up a file change) really needs to have multiple application server instances in order to handle potential failures of individual instances. Once you have multiple application servers to handle failures, you can also safely restart individual instances for maintenance without causing a problem.