Learning PHP

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

Learning PHP

Post by ConVicT » Wed Mar 25, 2015 10:01 pm

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: 542
Joined: Wed Oct 22, 2014 10:22 pm
Contact:

Re: Learning PHP

Post by aP|Nelg » Wed Mar 25, 2015 10:30 pm

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: 1004
Joined: Fri Feb 17, 2012 2:33 am

Re: Learning PHP

Post by ConVicT » Wed Mar 25, 2015 11:05 pm

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: 1638
Joined: Thu Oct 20, 2011 2:11 pm

Re: Learning PHP

Post by Light » Wed Mar 25, 2015 11:24 pm

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: 1004
Joined: Fri Feb 17, 2012 2:33 am

Re: Learning PHP

Post by ConVicT » Wed Mar 25, 2015 11:54 pm

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: 542
Joined: Wed Oct 22, 2014 10:22 pm
Contact:

Re: Learning PHP

Post by aP|Nelg » Thu Mar 26, 2015 12:11 am

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 » Thu Mar 26, 2015 6:11 am

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 » Thu Mar 26, 2015 1:39 pm

Nice tutorial

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

Re: Learning PHP

Post by aP|Nelg » Thu Mar 26, 2015 3:59 pm

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