Unsetting elements of array in a for loop in php

Posted May 27th, 2011 in Snippets by Metod

I did something like this today:

  1. <?php
  2. $arr = array("some", "elements", "in", "array");
  3.  
  4. for($i = 0; $i < sizeof($arr); $i++)
  5.     if(true) // for sake of clarity
  6.         unset($arr[$i]);

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.

Solution:

  1. <?php
  2. $arr = array("some", "elements", "in", "array");
  3.  
  4. $size = sizeof($arr);
  5.  
  6. for($i = 0; $i < $size; $i++)
  7.     if(true) // for sake of clarity
  8.         unset($arr[$i]);

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

Missing } in XML expression

Posted May 26th, 2011 in Razno by Metod

Today I got frustrated because when trying to parse a json string with jQuery, apparently it was causing a Missing } in XML expression error. But doing a bit more research I found the true problem. Boy was I wrong.

What was the problem?

  1. <script type="text/javascript"><script type="text/javascript"> …

And since Firefox and Firebug showed me purified and corrected HTML, I had no way of knowing that. So when you get this error next time, check for this situation. :)

How to stop a running cron job

Posted May 15th, 2011 in Razno by Metod

That was the question I asked myself when a running cron job on a shared hosting went horribly wrong. Turns out the solution is ‘pretty’ easy. You just have to know when the cron job started. In my case it was a PHP script. So i grep-ed for php also.

  1. ps -ef | grep php

You get the list of processes matching your criteria. Then you have to look for the time the script should have started. In the second column there is it’s PID. You can use that to kill it.

  1. kill $PID

Invalid property value javascript error in IE6 and IE7

Posted April 16th, 2011 in Razno by Metod

Recently I checked to see if my new project worked in IE6 and IE7. Well, actually I checked how it behaved. Obviously it did not work as expected.

The IE’s kept on bugging me with JS errors. IE6 said “Invalid property value”, while IE7 said “Unknown runtime error”. The code snippet was the following:

  1. el.style.color = "inherit";

And it worked in all browsers except in IE6 and IE7. As I learned later on, IE < 8 does not support the inherit value. So when changing the inherit to an actual color value, the errors were gone. So the next time you are bugged by this kind of errors in IE < 8, check for inherit values in your CSS, JS.

Phing executing multiple commands

Posted March 26th, 2011 in Zanimivosti by Metod

A little tip. If you deploy your PHP projects with Phing, you are familiar with the file build.xml. If you want to execute multiple commands, that depend on each other to be executed, you have to use && in between the commands. For example, clearing the cache:

  1. ssh user@example.com ‘cd cache && rm *’

The line in phing build.xml would look like this:

  1. <exec command="ssh user@example.com ‘cd cache && rm *’ />

But typing it like that will produce an error, since it is not a well-formed XML document. You have to transform the &’s into entities.

  1. <exec command="ssh user@example.com ‘cd cache &amp;&amp; rm *’ />

This will work as expected.

Return sfView::NONE in Symfony2

Posted February 20th, 2011 in Razno by Metod

In Symfony 1.x if you wanted to output some text or json or something else and not use a template for that action you would just do the following:

However, in Symfony2 that isn’t working anymore. Every action must return a response, so you can do the following:

New project: BoredTODO

Posted February 16th, 2011 in Projects by Metod

I managed to find some free time and finish my quick project – BoredTODO. It is essentially a TODO list which you can use when you get bored, so you have something to do. It takes advantage of some libraries and services:

Some facts:

Doctrine2 DBAL is a database abstraction layer, which makes it easy to work with the database on a smaller projects like this.

It is written in PHP 5.3.

jQuery was used to make all the shortcuts and ajaxy stuff work.

Reset CSS is in my opinion a must tool for every project, since it gives you a nice clean foundation to style on.

Google Fonts API was used for making the Ubuntu font family available in CSS across platforms and browsers.

The app is HTML5, just for fun.

It uses some CSS3 features for making rounded borders and gradients without pictures work. Therefore only few browsers are supported. Of course IE is not one of them.

You can see and try out the demo here

or

You can grab the source code on GitHub.

Requirements

PHP >= 5.3.0

Tested on UNIX system. Not tested on Windows.

Installation

In config/config.php change $connectionParams to your configuration.

  1. $connectionParams = array(
  2.     "dbname"   => "CHANGEME",
  3.     "user"     => "CHANGEME",
  4.     "password" => "CHANGEME",
  5.     "host"     => "localhost",
  6.     "driver"   => "pdo_mysql"
  7. );

and also change the basedomain configuration value.

  1. Config::$basedomain = "http://CHANGEME";

Usage

BoredTODO is all about keyboard shortcuts.

First off you will want to type in a short description of the task at the top. If you wish to add this task without any longer descriptions, press ENTER and the task is inserted.

BoredTODO offers you three priorities – low, medium and high. If you wish to add a priority to your TODO, type one of the keywords “low”, “med” or “high” followed by a whitespace in the short description. Examples:

  • low This is a task with low priority.
  • med This is a task with medium priority.
  • high This is a task with high priority.

But if you would like to add some detailed description to the task, press TAB and the cursor will be focued on a textarea in which you can type the description.

For insertion of the task press TAB again.

Your TODO is ready for usage.

If there is a long description, a pointer cursor will appear when you hover over a TODO item. If you click on it, the description will show up. If you click on it again, it will disappear.

When you want to close the task, you have to click on the most-left side of the TODO – the priority color. When you do that, the TODO is checked (deleted). But if you made a mistake, you can easily undo the action by clicking on the priority color again. The TODO is back.

Browser Support

Since BoredTODO uses some CSS3 features it supports only the latest and non-IE browsers:

  • Firefox
  • Chrome

Untested:

  • IE9
  • Safari

Opera 11 does not seem to have support for CSS gradients, so it is not fully supported. BoredTODO might function on other browsers as well, but was not tested.

Code License

MIT License

Copyright © Metod

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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:

  1. echo $sth;
  2. return;

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:

  1. exit($sth);

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:

  1. 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.

Porting PHPEclipse syntax coloring to Eclipse PDT

Posted February 1st, 2011 in Razno by Metod

I was pushed into the “eclipse php world” with Eclipse + PHPEclipse setup. Although I knew about Eclipse PDT I never bothered to install it since PHPEclipse worked fine. Up untill I wanted to work with the newest PHP 5.3 features. PHPEclipse just doesn’t support that. So I had to move to PDT. It supports PHP 5.3 and is indeed a better tool. But what bothered me was the syntax coloring which is a bit different from PHPEclipse. So I’m publishing this “migration” post for anyone who has or will have the same problem. You can also use this guide to migrate to Zend Studio, since it is based on Eclipse PDT. Including me at some future time.

Eclipse PDT / Zend Studio:

  • Multi-line comment: #3F7F5F
  • Single-line comment: #3F7F5F
  • PHP tags: #FF0080
  • Keyword: #7F0055
  • Functions: #7F7F9F (no italic)
  • Variable: #7F7F9F
  • Parameter variables: #7F7F9F
  • Fields: #7F7F9F
  • Static fields: #7F7F9F
  • Superglobal variables: #7F7F9F
  • Internal constants: #7F0055
  • Constants: #7F0055
  • String: #2A00FF

Also in Zend Studio, line highlight is not the same as in PHPEclipse or Eclipse PDT.

  • Current line highlight: #E8F2FE

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:

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

And I found a way which is quite nice:

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