What is PHP - A beginner question

What is PHP - A beginner question
0

#1

I am a beginner to web development. I learnt html, css, js. Now i came to know PHP is also used for web development.
So , Where ,why is it used?
What is it’s pros and cons? and so on…


#2

It’s a scripting language that works on servers. It was written as a way to make HTML pages dynamic, as a templating language. So you write an HTML page, and you give it the .php extension instead of .html, and you put it on a server that understands PHP, and you can add special syntax which can allow you to do a variety of things HTML cannot do on its own (upload data from forms to a database for example).

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <?php echo "Hi, I'm a PHP script!"; ?>
    </body>
</html>

That would just print out the text in the <?php ?> tags. Anything inside those tags is PHP script, the PHP interpreter will convert/run anything inside them.

It was written so that the author could make webpages do things like interact with databases, so he could say have a form on the web page that worked, that a user could fill in, hit submit, and would put the stuff in it into some kind of data store.

It turns out this is a very useful thing, and using PHP was much, much easier than any of the other options at the time (Perl, CGI scripts, Java among other things). It kinda grew uncontrollably from there: because it was very easy to use (you just need a web server with a PHP interpreter on, and you can just upload all the files and they basically Just Work), lots of people used it, the community of users grew enormously, and built more and more things (kinda like JavaScript today). It wasn’t built as a “proper” programming language, but it turned into one because of that.

Pros:
It is very easy to build basic things with.
It is easy to learn.
It works almost everywhere.
Deployment is basically as simple as uploading the PHP files to a server.
It has a huge ecosystem and vast amounts of material to help learn the language.
It is well suited to building CRUD web applications if those applications are simple.

Cons:
It turned into a “real” language kinda accidentally, and is a massive mess (much work has been done to try to fix this).
Following on from this, it’s not so much badly designed as not actually having any real design; lots of things in the language are wildly inconsistent.
Applications written in PHP have a reputation for having awful codebases (see WordPress for example). This is possibly related to the fact it is easy to learn, and lots of people built crap in it that got popular ¯\_(ツ)_/¯
https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

If you’re starting out learning programming, there are better languages (ie most of the other ones); PHP has its uses, and for historical reasons is still very, very widely used, but jobs-wise it is I think fair to say that things are trending away from the language as better alternatives have become commonplace (and ability to deploy applications written in those languages has become easier). It is easy to build simple things, the leading framework (Laravel) is quite good, but the language has systemic problems. Putting everything else aside, it won’t teach you to write good programs as well as a well-designed language will: doing that won’t be as easy initially, but will pay dividends in the long run. If you just want to shove a very basic web page up, go for it though.


#3

I have used PHP for years and do not see how it is such a bad language. Can you explain in more detail how another language specifically is better.

And it is so much easier to implement than node etc. There are 100s of hosters that have the PHP server and configs already set up and all you have to do is upload the file. And many of the are free.

I have been looking for a hoster where you can do that with code.js etc and cannot find it.

Is there something I am not seeing here?


#4

Hello @Win_Key. PHP is a server-sided programming language as is Python,Ruby and Node.js .I once tried Python and PHP but the one that was abit easy to understand was Ruby.They have an easy to pick framework called Ruby on Rails.


#5

Hello @DanCouper. How are you doing?I have a burning question that i would kindly ask you to help out with.I decided to attempt the back-end challenges where they require you to open a cloud9 account.

I was able to set the account successfully and even created a workspace using the node template.But i didn't know how to navigate the command prompt so i messed up with it.When i create a workspace, I do not get the preview and run buttons at the top right corner of the editor.When i run the linux commands,i get an error that states "failed to write to project.settings".I have even tried to read their documentation but can't seem to figure out how to reset the workspace to the previous state so i can be able to preview the app that i am making.Kindly help.Thanks.


#6

@Win_Key,if you would like to have a sense of how Ruby and Ruby on Rails feels like,visit here and learn .


#7

I’m not sure how I can explain more than I already did:

Pros:
It is very easy to build basic things with.
It is easy to learn.
It works almost everywhere.
Deployment is basically as simple as uploading the PHP files to a server.
It has a huge ecosystem and vast amounts of material to help learn the language.
It is well suited to building CRUD1 web applications if those applications are simple.

Cons:
It turned into a “real” language kinda accidentally, and is a massive mess (much work has been done to try to fix this).
Following on from this, it’s not so much badly designed as not actually having any real design; lots of things in the language are wildly inconsistent.
Applications written in PHP have a reputation for having awful codebases (see WordPress for example). This is possibly related to the fact it is easy to learn, and lots of people built crap in it that got popular ¯_(ツ)_/¯
https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

Regarding deploying Node, literally just type “node hosting” into Google. It’s a newer platform (2009, 15 years after PHP) so less hosts have it installed as standard (note all hosts will install it if you pay, but always the case for anything), but it’s still extremely common, same as Python and Ruby only became standard on hosts a few years ago, years after they came into existence.


#8

Thanks for your answer and I have seen that and you just pasted it.

But it seems like your answer is ciruclar: I asked why is PHP so bad and you answered ‘its not real, badly designed, inconsisent, massive mess’ but no concrete examples.

If you do not know that is OK because I have never worked with node etc so I sure do not know either. But if I could get a concrete example from someone how PHP is so bad rather than ‘its bad cuz it’s a mess’ that really does not tell me anything.

I know just as one comparison that is is VERY easy to print ‘hello world’ in PHP while it is a mess in node.

I wonder how much of the new languages are just a fad. To me even MVC and some of these frameworks are just overkill and the apps could be created without them if a person codes well. It is a nightmare to print ‘hello world’ in laravel.

But like I said I really do not know. That is why I am asking people.


#9

example of inconsistency : array_map() and array_reduce() functions have arguments in different order, there is also inconsistency in function naming like str_replace() and strtolower().

This is annoying if you don’t have an IDE with autocompletion.


#10

nk, thanks for the response!

Seems to me these are very minor issues. I am sure I could find equal annoyances in node.js etc.

And can you give me a practical example where array_reduce would be used?

I think one of the most commonly used applications is CRUD. I know it is very easy to set this up in PHP while in node.js (so far as I can see) it is a nightmare. Also overkill in laravel, and other MVCs.

Again i think we can judge a language quite a bit on how it prints "hello world’ to the screen.

Appreciate your input! ty!


#11

Node.js, or even JS in general has many frustrating things we must cope with, it’s similar with PHP quirks. PHP is here to stay, we can love him or hate him, but it’s inevitable for many web dev jobs.


#12

I’m a JS developer, and I don’t think JS is a terribly good backend language, it’s hugely flawed, but Node is a good tool in certain cases. It’s not a fad, it’s very good at specific things. PHP is good for easily shoving up relatively simple sites and web apps.

Node isn’t designed to be good for building CRUD apps. Node is designed for IO-heavy and real-time systems; it doesn’t block on IO, which is important. PHP is not good at the things Node is good at.

And MVC - MVC is just a pattern, which yes, can overcomplicate simple things. But at the same time, once you need something that would be a good fit for that pattern (eg often big CRUD apps), it becomes very useful.

There’s a link to huge list of issues in PHP that I posted. That’s from 2012, and efforts have been made to fix many things (as I said), but nobody but nobody seriously denies that the underlying language is a mess. And yes, as I said, the pros are it is very easy to build simple web stuff, which covers a huge area - I never said it wasn’t useful. It just happens to also have extremely well-known intrinsic flaws.

Anyway, so, Node is an environment for executing Javascript. Javascript is the language, and at its most basic “hello world” is console.log('Hello World'), or document.body.innerHtml('Hello World') or similar.

Node [w/r/t a web application] is run as a server; you have a [JS file] entry point, and Node can execute that. Its stdlib contains all of the components needed to do this. You don’t put Node on a server (like PHP on Apache), Node is the server. To do “hello world” in Node, you write a very basic server and get that to send an HTTP request containing the text ‘hello world’ -

const http = require('http'); // import the http module from Node's lib

const hostname = '127.0.0.1';
const port = 3000;

// the http lib has a function called createServer, which takes a callback
// which itself takes 2 arguments, for the request and the response objects.
// Only care about the response in this case (hit sever on localhost, get a response)
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

// Tell the server to listen on the port specified
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

The equivalent in PHP is not “write hello world in literal PHP”, it is do that and setup and configure an Apache server.


#13

Here’s what I recall on the history of PHP and it’s relation to other scripting languages.

Back in the early days of Internet, the only way to have some semblance of interactivity was calling Perl/CGI programs from your html page. CGI == Common Gateway Interface. This external Perl program basically can accept input strings, do some processing and decisions and output back a string to the browser. This naturally run on the Linux server or any of the Unix flavor boxes at the time. Programs/Perl scripts would usually reside in a /cgi or /cgi-bin directory.

When Microsoft tried to get in on the Internet game, they of course provided their own CGI equivalent. They called it /win-cgi and it’s basically a compiled .DLL file, outputted by their Visual Basic software. Unlike Perl/CGI programs, this was very easy to understand and write your own programs… it was basically BASIC language-like. The nice thing is you can even upload a Microsoft Access .MDB file to your web server, and be able to read/write to that Access database using Win-CGI. This was very powerful back then (around mid 90s). Now, website interactivity isn’t just limited to simple text processing like Perl/CGI, but now you have the whole syntax/functions of Visual Basic available to you, PLUS database functionality! You can update records, browse, search, update database records online! Of course, this only works on Windows server only.

Problem with win-cgi, is the need to have compiled DLL. If you have to change one thing, you have to go back to your source code, recompile source code to DLL, then upload DLL to web hosting… rinse and repeat.

So later on, Microsoft came up with ASP (Active Server Pages). So basically, the same BASIC like syntax can now be executed by each webpage without the need for DLL compilation… i.e. an interpreted scripting language. There’s no need for compilation. You can make changes to the source code, upload your .ASP file to the web host, and voila! It works. Again, it’s a Microsoft/Windows server only solution.

Of course, on the Linux side, previously they only had Perl/CGI… until some guy created a similar scripting language in answer to Microsoft’s .ASP. They called it PHP (back then, it stood for Personal Home Page). Like .ASP, it’s a scripting language that is executed by each page and there’s no need for compilation. And it runs on Linux! – for some people, that’s enough reason to use it.

In Microsoft ASP, you have

<% Response.Write("Hellow World") %>
<%= ThisValue %>

and in PHP, you have

<? print("Hello World") ?>
<?= $ThisValue ?>

See the similarities? I tend to think of PHP as the equivalent to Microsoft’s ASP page.

blah blah blah… lots of things happening on the Internet… blah blah blah

Microsoft eventually moved on from .ASP scripting language that is Visual Basic-like and went on to ASP.NET WebForms, then .NET Framework and eventually ASP.NET MVC. Early on, they’re big on VB, but later on, it seems they want everybody to be on C# which is a fully object oriented/class base language, with strict type checking, etc.

PHP meanwhile seemed to have stuck there… going from version 3, to 4, 5.x and in each version interation, growing unwieldly and sometimes inconsistent to retain backwards compatibility. It’s still uncompiled, still a page based scripting language. Very easy to learn, very easy to get up and running. This backwards compatibility and popularity made it a choice for lots of programmers and everybody from no-name Joe to Mark Zuckerberg used it to create lots of software, like CMS, Wordpress, bulletin boards, facebook, etc… etc…


#14

Dan, thanks for your well expressed response!

I have not done the new languages but have lot of experience in ones before this last crop like node.js and I am getting a gut reaction. I had this same reaction to Windows NT server and could not get it. Then finally MS admitted that NT server was a dog and based on a faulty concept.

Yes I realize that an apache server has to be set up to do PHP but that is so much easier than node.js.

’ Node is designed for IO-heavy and real-time systems; it doesn’t block on IO, which is important. "

Can you give me an example of this?

I cannot help thinking that the MVC and Laravel etc are crutches for people who do not really know how to code.

For instance I am not a big fan of OO. And I have researched this quite a bit and many feel that using functions is better or at least as good.

I do think that IT is very fad driven. Jobs made billions on tapping into that. How many android apps are just wastes of time that could be done much better manually?

I know all the cool kids use node.js and apples.

Well I am still investigating. I am planning to learn node.js. Seems just to get ahead one does have to try to be one of the ‘cool kids’. I have seen this happen in the past. Hiring managers hear the latest buzz words and when someone has this on their resume or brings it up in a interview he knows you are one of the cool kids.

But thanks again for your very good explanation and I will be studying what you have said.


#15

Thanks for the excellent history lesson. Thorough but concise!


#16

owel,

Thanks for the excellent history lesson. Thorough but concise!


#17

You are right in that there are fads (but then [say] Java was a hot new fad language in the mid-90s, and now it’s used everywhere), but you also have to bear in mind that a lot of apps do basically the same thing - having a pattern that can be applied means a. you aren’t reinventing the wheel every time, b. it is easier to hand to another programmer (it is much easier to say read the Laravel documentation to figure out how a complex app works than spelunk through someone’s custom code). PHP is itself an abstraction - you could take the argument further and say people could just write C bindings themselves, but it gets ever more difficult for other programmers to understand and maintain the lower level one gets

So the way Node works best is by leveraging JS’ asynchronous event model. Code that would block (ie you take some input, that gets processed, then the program continues; while it’s being processed, the program ‘blocks’) can be written so that the asynchronous parts get scheduled whenever JS can deal with it. You can say make a request to a DB, and the program will continue running, and when the response comes back, that will be fed back into the event loop.

This is tends to be quite hard in some languages, but because of the way JS works, it becomes a lot easier, code can be written that looks synchronous (so is easier to write - do this then do that then do another thing), but occurs asynchronously. Examples of usage would be a chat app, where you have lots of users in a chatroom, and they are all sending messages: you don’t want the world to stop whilst every person finishes writing their message, you just want everything to carry on and the messages to come through when they can. There are event-driven frameworks for most languages - Kraken is an example of one in PHP - but it’s always going to be easier if the language and the environment it’s written in are built from the ground up to support the thing you want to do.

Here’s a pretty good Stack Overflow answer explaining the model: https://stackoverflow.com/a/14797359/1724802


#18

Dan,

Thanks again for the well thought out response!

Well Java never was a fad language by definition. And I am sure not saying lower level is better. I think the higher the better as long as speed etc are not affected adversely too much.

OK so JS can ‘multi-task’ while PHP cannot? Am I understanding this correctly. If that is true then JS of course would be better in many situations. Seems like for very large scale apps.

Are you saying that Laravel and MVCs are useful because they are self documenting?


#19

I suppose patterns of structuring apps make thing self-documenting to some extent. But I meant more actual documentation. I haven’t really used Laravel extensively, but I have used the framework it’s based on, the MVC-like Ruby on Rails. And one RoR app looks very much like any other RoR app: if I pick up someone else’s work, I know where the parts of the app live. If I give someone else something I’ve worked on, even if they’re not familiar with RoR, it’s very easy to explain, and they can refer to extensive documentation as why things work and where the code for them lives. It makes development much, much faster, easier, less error-prone.

Re JS, it’s important when comparing it to PHP to bear in mind what @owel said, PHP is for paged web applications. But maybe you want something that isn’t a paged application - a good simple example is the FCC weather project. People had a helluva job doing it using Codepen due to CORs issues. So the solution FCC used was to write a proxy server, in Node, and literally all it does is when someone hits the given endpoint, it makes a request to the weather service, which then returns the JSON response, which is then sent on to the client. Node’s really good for this; it’s dead easy to set up, dead easy to get running, the solution is basically a single file with a few lines of code. That’s not to say there might be better languages, but Node makes it super easy.

And there are loads of things that aren’t paged web applications that aren’t massive things. e.g. web APIs are very common, and they’re very easy to create in Node.

Note VSCode and Atom are Node

And it doesn’t matter how good someone is at coding, their codbases will always be awful thing for other people who have to work on them. Abstractions like patterns and frameworks add consistency


#20

Dan,

very good points! thanks for the response! more than i can digest quickly will have to study your comments.

could you explain in more detail what are ‘paged applications’ and ‘COR issues’?

crazy as it may appear I still think there is a demand for COBOL coders, a language whose death was predicted at 1985.