r/PHPhelp • • Nov 05 '18

PHP for other stuff than web development

So I started working for a software company a month which is great, only they PHP for almost everything.

So my question is, if there is any book or online tutorials that teaches PHP but for other stuff than web development?

7 Upvotes

24 comments sorted by

4

u/[deleted] Nov 05 '18

I'm guessing you're talking about running php in terminal? If that's the case everything is the same except your entry method may have done arguments passed in. There's no need for a book on that as it's a 2 minute read on Google. God luck with your new job!

1

u/benner6 Nov 05 '18

thanks man!

4

u/[deleted] Nov 05 '18

Laravel Zero is quite a decent way to run non-web php for scripts etc. They've stripped Laravel down to the bare minimum for command-line running - without HTTP, database models, .env support etc, which you can load in from composer packages as you need.

2

u/benner6 Nov 05 '18

Wow thanks, I'll take a look at that!

2

u/[deleted] Nov 06 '18

Just discovered Zero recently. Going to re-write all of my current scripts.

3

u/eurosat7 Nov 05 '18

It makes close to no difference if you output to a browser or a command line.

I would only like to point out this two things:

var_dump($argv);
$stdin = fopen('php://stdin', 'r');

1

u/benner6 Nov 05 '18

I have seen some of the guys at the company use var_dump and I have played around with fopen, but can you explain the $stdin part?

3

u/Krauter123 Nov 06 '18

Symfony has a console component which you can use without all the other things. I really fell in love with it, its quite awesome, especially when you can reuse your doctrine entities for example. Makes an easy way to build quite heavy command line apps. Maybe that would be something for your team :-)

1

u/benner6 Nov 07 '18

Yeah, it sounds pretty cool, I'll have to look into this as well when I get the time

1

u/gevrik Nov 09 '18

I second the Symfony console. Perfect for rapid development of console applications.

https://symfony.com/doc/current/components/console.html#creating-a-console-application

2

u/Mike312 Nov 06 '18

Lots of my early experience with php was secondary processing of mailing lists (I worked for a bulk mailing company). For the most part the web gui was unstyled and pretty spartan, but I had tons of cool scripts for splitting up mailing lists for clients in addition to the normal address correction stuff we did. Lots of it was upload a CSV and a way to filter the list and it would return a link to download the result CSV (or email it to me). I don't think there's really any literature that teaches using PHP outside of web development because when you learn PHP you learn server-side code and how to apply that to the web. If you can PHP, you're already doing it outside of web development, and web development is the additional step.

1

u/benner6 Nov 06 '18

When you put it that way, then it makes a lot og sense

1

u/robske_110 Nov 05 '18

Probably a bad project to look at; But for an example of a big CLI application take a look at PocketMine-MP, a GAME server written in php. Seriously. You won‘t belive what php can do. (And the software was even around in php5 times, where it was so awfully slow.) The thing which makes that even possible is pthreads. (A php extension for cli which makes it possible to use threads in php [Real threads, no forking going on here!]) https://github.com/pmmp/PocketMine-MP I don‘t know if that helps on you some practises used in cli app development, especially because it might contain some bad practises.

1

u/benner6 Nov 05 '18

I'll look into it, it sounds very interesting

2

u/wh33t Nov 05 '18

That's interesting. I've often found myself reaching for php instead of writing shell scripts to do some system maintenance stuffs and wondered if many others did it.

1

u/aasmith26 Nov 05 '18

I do!

1

u/wh33t Nov 05 '18

Nice, what kind of things do you do with it? I mostly use it to do regular rsync backups and database maintenance.

3

u/[deleted] Nov 06 '18

I use it for various purposes. Some examples:

  • Watch files for changes in situations something needs to happen when a file changes (usually invoke the script with cron)

  • parse logs and submit relevant parts to a database - I built my own bandwidth meter to monitor sites bandwidth usage by parsing a CustomLog from apache

  • I have a little cli password generator utility I use nearly every day

  • little utilities that automate parts of websites, usually invoked by cron on an hourly/daily basis

I also use a lot of Bash scripting, but anything more advanced than simply restarting services, running rsync, or clearing tmp files, I use PHP.

1

u/farfromunique Nov 06 '18

watch files for changes

What does this look like? Not just "what functions" but also "how do you run it"?

2

u/[deleted] Nov 06 '18

Well, there's a few ways.

One would be to launch it as a daemon. Basically you wrap the majority of the script in a while(true) loop, with a sleep(10) on the end.

Another route would be to store some form of file list with the state info (file size, hash, etc) then run it with cronjob every few mins but each time need to load the state file and then compare on each run, then update the state file.

Another way on Linux would be to use inotify tools.

1

u/[deleted] Nov 06 '18

On my local system,

- Parse the log files every morning
- Query various local services at work to get the status and locations of various dev/qa/test platforms.
- Inject headers into SQL dumps to improve re-import (DEV Only)
- Update all of my various repos

What I can do...
- Change the directory. Seriously, but I am unable to do it with python either. Using Windows 7 (don't ask) I can't write a script that will CD into a project directory and quit. As soon as the script quits it is back at the old script. Current solution is to create an alias file based on the projects installed in my system.

1

u/Tokkemon Nov 06 '18

I use it for file handling in particular all the time.