Learning PHP

Everything todo with programming goes HERE.
Post Reply
User avatar
ConVicT
Shutout Match Winner
Posts: 1001
Joined: Fri Feb 17, 2012 2:33 am

Learning PHP

Post by ConVicT »

Just started to learn php and was wondering if someone could give me a tron script they've made, so that I can try and figure out what it does just by reading it.

Not anything too hard yet, only learned a few things.
User avatar
aP|Nelg
Match Winner
Posts: 621
Joined: Wed Oct 22, 2014 10:22 pm
Contact:

Re: Learning PHP

Post by aP|Nelg »

This is a pretty simple script, I made it in my earlier years of php when I didn't use tabulars yet, I would say about 2013.

Code: Select all

#!/usr/bin/php
<?php
$player_enter = array(
"Hello",
"Hi"
);
While(!feof(STDIN)) 
{
  $line = rtrim(fgets(STDIN, 1024));
    $split = explode(" ", $line);
  if(preg_match("/^PLAYER_ENTERED/", $line))
  {
    $val = rand(0,(count($player_enter)-1);
    echo "say {$player_enter[$val]}\n";
  }
  if(preg_match("/^SHUTDOWN/", $line)) exit();
}
?>
User avatar
ConVicT
Shutout Match Winner
Posts: 1001
Joined: Fri Feb 17, 2012 2:33 am

Re: Learning PHP

Post by ConVicT »

Cheers, Nelg, keep um coming.
I can gather that it randomly says "Hello" or "Hi" when a player enters, but that's most likely all I get so far. There's few things there I've not learned yet.
Am I right in thinking that I'd have to learn whatever commands and directories to make scripts for tron (or any other game).

Another thing I'm wondering (dunno if it sounds daft or not... Learning). Is there variables that I can't use because they'd conflict?

Btw, a lot of what I ask may sound stupid, I'm only 50% through the PHP lessons on Codecademy, thank's for directing us to it, Sinwav.
User avatar
Light
Reverse Outside Corner Grinder
Posts: 1667
Joined: Thu Oct 20, 2011 2:11 pm

Re: Learning PHP

Post by Light »

Here's a tutorial I wrote about a year ago ..
http://forums3.armagetronad.net/viewtop ... 12&t=23539

And here's little script for map rotation without any explanation ..
http://forums3.armagetronad.net/viewtop ... 32#p280733
User avatar
ConVicT
Shutout Match Winner
Posts: 1001
Joined: Fri Feb 17, 2012 2:33 am

Re: Learning PHP

Post by ConVicT »

Thanks.
I had a quick read through it and it looks really helpful.
I'll have a thorough look tomorrow, it's starting to give me a sore head lol.
Think I'm trying to work things out that I've not yet learned, doesn't help.
Last edited by ConVicT on Thu Mar 26, 2015 2:06 pm, edited 1 time in total.
User avatar
aP|Nelg
Match Winner
Posts: 621
Joined: Wed Oct 22, 2014 10:22 pm
Contact:

Re: Learning PHP

Post by aP|Nelg »

A sidenote: You should probably use
While(!feof(STDIN))
instead of
While(true) or While(1) etc etc

Why?
If the server shuts down with While(True) it will generally eat your CPU and RAM, which I'm sure is a problem in dukevin's scripts...

Thank you to Durf for pointing that out in my scripts. I wouldn't have known because I originally learned php from looking at other scripts initially. (Dukevin's... go figure... I guess it just shows how easy PHP is)
User avatar
dukevin
Round Winner
Posts: 219
Joined: Mon Aug 30, 2010 8:25 am
Location: Southern California
Contact:

Re: Learning PHP

Post by dukevin »

aP|Nelg wrote:A sidenote: You should probably use
While(!feof(STDIN))
instead of
While(true) or While(1) etc etc

Why?
If the server shuts down with While(True) it will generally eat your CPU and RAM, which I'm sure is a problem in dukevin's scripts...
No that doesn't. The former method is only useful if you terminate scripts with KILL_SCRIPT, I use screen sessions.
aP|Nelg wrote: I originally learned php from looking at other scripts initially. (Dukevin's... go figure... I guess it just shows how easy PHP is)
You make it sound like my scripts are bad...
@Convict I have a tutorial here: http://rxtron.com/scripts/tutorial.html
Image
Durf
Match Winner
Posts: 426
Joined: Mon Jul 30, 2012 10:35 pm

Re: Learning PHP

Post by Durf »

Nice tutorial
User avatar
aP|Nelg
Match Winner
Posts: 621
Joined: Wed Oct 22, 2014 10:22 pm
Contact:

Re: Learning PHP

Post by aP|Nelg »

dukevin wrote:No that doesn't. The former method is only useful if you terminate scripts with KILL_SCRIPT, I use screen sessions.
Well it does with my setup though. (and it doesnt use SPAWN_SCRIPT/KILL_SCRIPT) though I'm thinking about switching to it since I fixed the bug in the CP.
dukevin wrote:You make it sound like my scripts are bad...
Sorry about that
Post Reply