Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Andres Vidoza
Andres Vidoza
9,805 Points

Hey guys, I am a bit confused about PHP. How come I can run PHP script on the terminal when it is a server-side script?

I am currently taking PHP for beginners. I don't quite understand why PHP can run on the terminal if it is a server side script. Does the terminal have an interpreter for PHP? if yes, how come the web browser doesn't (for testing purposes of course)

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Not sure what you are asking. What do you mean it can't run on the web without a server? When you run PHP on your machine in a terminal session you are using the php CLI (the command line interface) as opposed to serving up web pages to a browser. One example of running php on a web server is a cron job.

Andres Vidoza
Andres Vidoza
9,805 Points

I don't quite get why it can run on the command line Dave..

Thank you!

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Andres,

It is a feature of the php cli. It might help to check the documentation PHP command line, especially the sub section on the Built-in web server

2 Answers

The best way to run a php script in a web browser would be to install a package like xamp or wamp. You can run your script in a browser and it will be exactly what the client that uses the script will see.

https://www.apachefriends.org/download.html https://sourceforge.net/projects/wampserver/

Are two seperate projects that will let you do this.

Also, if you are running a script that doesn't require a server, you can run the php script from the command line.

Like this:

php scriptName.php

Andres Vidoza
Andres Vidoza
9,805 Points

what script would require a server tho? How come I can run it from the command line if its supposed to be a server side language.

Thank you for the info by the way!

Hi Andres! I believe what you're asking is a reason why php scripts are able to be run from the command line. Forgive me for the late response as well. This question came up and I thought I would stop by for anyone else wondering this. By now you are probably far along in getting to know PHP though!

PHP is a server-side language, that's true, but isn't limited to being used only in such a capacity. Once I had an excel spreadsheet that I wanted to convert to JSON, just another form of data but one that I could then reference a lot easier in code. I used PHP to do that and ran the script I had to do this with the PHP command-line interface. The terminal is able to run PHP scripts because the computer's operating system is able to read and interpret PHP code (perhaps with some configuration beforehand). Dave also mentioned a CRON job, where a script or bit of code is ran at specific intervals for any length of time, which can have a great amount of uses in areas outside of web development.