(PHP) Code snippet #6

Posted May 26th, 2016 in Snippets by Metod

A while ago I encountered thle following one-liner snippet in a legacy codebase.

Yes, it was a one-liner! Alarming. So, what does it do? Let’s tear it apart and see.

Hint: it displays the name of the first element in the array. But it still iterates through the rest of the array. Which might be ok if the array is of length 1. But still there are some issues left:

  • The complexity.
  • The unreadability.
  • Wrong structure (array instead of string) for the job.

All of the above can be solved by moving the logic somewhere else:

Keep it simple, stupid!

Post messages to a Teamspeak3 instance

Posted September 9th, 2012 in Snippets by Metod

This is a simple script for posting messages to a Teamspeak3 instance.

It uses the TeamSpeak3 Framework.

Symfony2 tip: Get current route name in twig template

Posted December 5th, 2011 in Snippets by Metod
{{ app.request.attributes.get('_route') }}

Enjoy! 😉

Code snippet 6

Posted October 19th, 2011 in Snippets by Metod

My (R):

// safety switch 🙂

Code snippet 5 – CoD1 modding

Posted October 1st, 2011 in Snippets by Metod

I was administering a Call of Duty 1 server a while back and wanted a rename command for use through the remote console in AWE 2.12 mod. So I dug into the code of the admin tools. And wrote the following function:

	self endon("boot");
	setcvar("g_rename", "");
		if(getcvar("g_rename") != "")
			player_id = getcvarint("g_rename");
			for(i = 0; i < level.awe_allplayers.size; i++)
				thisPlayer = level.awe_allplayers[i];
				if((thisPlayer getEntityNumber()) == player_id && (substr("Renamed", thisPlayer.name) == -1 || thisPlayer.name.size > 11))
					level.awe_allplayers[i] setClientCvar("name", "Renamed " + randomInt(1000));
					thisPlayer iprintlnbold(level.aweallplayers[i].name + " ^7renamed by admin");
					iprintln(level.awe_allplayers[i].name + " ^7renamed by admin");
			setcvar("g_rename", "");
		wait .05;

Nothing special really, but it is an interesting look into how scripting works for Call of Duty 1.

Change keyboard layout in CentOS in console

Posted September 3rd, 2011 in Snippets by Metod

Quick tip. How to change keyboard layout in CentOS in console?

# vi /etc/sysconfig/keyboard

and change the KEYTABLE key. All available options can be found in subdirectories under /lib/kbd/keymaps/i386.

After that, just restart the machine.

# shutdown -r now

Unsetting elements of array in a for loop in php

Posted May 27th, 2011 in Snippets by Metod

I did something like this today:

The loop never came to the final element of the array, therefore not checking all of them. What was the problem?

Since sizeof($arr) is calculated every time the loop comes around, it was returning less and less with every unset instead of returning the same value.


That way you ensure that size is always the correct integer. And it will run faster.

Code snippet 4 – echo and return

Posted February 9th, 2011 in Snippets by Metod

This one is in PHP. I stumbled upon a case, where I wanted to echo something and then return, so that execution of the script would stop. One way to do this was:

echo $sth;

But this was a 2-liner. I wanted to do this in a nice 1-line way. So one option to do this could be:


Which still feels wrong because in my opinion this is something in the line of the abuse of the exit construct. Besides, since PHP 4.2.0, exit construct does not print out the contents if the contents is actually an integer. So I found a solution to my problem:

return print $sth;

This line does exactly what I wanted. Outputs the contents (even if it is an integer) and breaks current script’s execution.

Code snippet 3

Posted January 15th, 2011 in Snippets by Metod

At the moment I am doing some java programming. And since being primarily a php guy, I was wondering how to easily return an array from a method like you can in php:

public function returnArray() {
    return array(1, 2, 3);

And I found a way which is quite nice:

public int[] returnArray() {
    return new int[]{1, 2, 3};

Access component variables in it’s controller in symfony 1.0

Posted December 16th, 2010 in Snippets by Metod

Today I was wondering how to access variables in a symfony component controller that were passed there through third parameter in a include_component() call. So I found a solution and am posting this as a reminder to me or anyone else who is stuck with symfony 1.0 and is wondering the same thing. You can call this method inside your class someComponents extends sfComponents.

$vars = $this->getVarHolder()->getAll();

Returns an associative array of variables.