Aragon Online

Go Back   Aragon Online > Discussion > Programmer's Cubicle

Programmer's Cubicle A place where some code snippets and other nerdy things will be posted for the general programmer community's benefit.
Note that this is not a programming HELP forum. Posts not about the code here will be deleted or moved.

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2010, 09:45 PM
Merlin's Avatar
Merlin Merlin is offline
Supreme Wizard
Administrator
 
City: Riverton
Race: Human
Wealth: 870,002
Population: 1,582
 
Join Date: Dec 2003
Location: The deepest dark of Oregon
Posts: 3,265
Merlin is on a distinguished road
Default PHP tips

I struggled with a bug, that I tracked down to a series of nested if/else statements. I had a block of code that gathered defenders.

My code initially looked like this:
PHP Code:
if (($def['tactical_x'] == $dx)
 && (
$def['tactical_y'] == $dy)
 && (
$def['hits'] > 0)) 
    if (
$spell_use[0] == 'F'
        if (
in_array($def['player_id'], $allies)) { // <-- else associates with this if
            
$defender[] = $def;
        }
    else 
        if (! 
in_array($def['player_id'], $allies)) {
            
$defender[] = $def_unit;
        } 
In the original code, no grouping was required, but changes introduced a bug. In spite of my pretty indentation, the else associated with the last if instead of the $spell_use. Doh!

So, note to self: always include braces!
PHP Code:
if (($def['tactical_x'] == $dx)
 && (
$def['tactical_y'] == $dy)
 && (
$def['hits'] > 0)) {
    if (
$spell_use[0] == 'F') {
        if (
in_array($def['player_id'], $allies)) {
            
$defender[] = $def;
        }
    } else { 
        if (! 
in_array($def['player_id'], $allies)) {
            
$defender[] = $def_unit;
        }
    }

__________________
Do not meddle in the affairs of wizards, for they are subtle and quick to anger.
The Lord of the Rings
Gildor, Chapter 'Three is Company'.
Reply With Quote
  #2  
Old 02-14-2010, 01:38 AM
Merlin's Avatar
Merlin Merlin is offline
Supreme Wizard
Administrator
 
City: Riverton
Race: Human
Wealth: 870,002
Population: 1,582
 
Join Date: Dec 2003
Location: The deepest dark of Oregon
Posts: 3,265
Merlin is on a distinguished road
Default Watch out for PHP's habit of sending a copy of an array

One of the problems I've had with the DB class is that when you pass an array to a function, that often is passed as a copy of the array, so modifications to that array in the function don't get propagated to the original.

Use an ampersand (&) to create a reference to the original in the function arg list, so changes are made to the original array.
PHP Code:
function modify_array(& $the_array$copy_array) {
    
$the_array['item'] = $new_value;  // <-- this will modify the array in the caller
    
$copy_array['item'] = $new_value// <- this modifies only the local copy

__________________
Do not meddle in the affairs of wizards, for they are subtle and quick to anger.
The Lord of the Rings
Gildor, Chapter 'Three is Company'.
Reply With Quote
  #3  
Old 08-17-2010, 01:32 AM
Merlin's Avatar
Merlin Merlin is offline
Supreme Wizard
Administrator
 
City: Riverton
Race: Human
Wealth: 870,002
Population: 1,582
 
Join Date: Dec 2003
Location: The deepest dark of Oregon
Posts: 3,265
Merlin is on a distinguished road
Default

There is a PHP option to allow a short tag "<?" instead of the longer "<?php", but for portability, use the longer tag, as support for the short tags varies by host and server.
__________________
Do not meddle in the affairs of wizards, for they are subtle and quick to anger.
The Lord of the Rings
Gildor, Chapter 'Three is Company'.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A PHP Google Chart class Merlin Programmer's Cubicle 1 10-25-2014 12:47 AM
A PHP Database Class Merlin Programmer's Cubicle 10 11-29-2013 12:16 PM
Bizarro post thread D.tauro General 26 12-17-2009 06:04 AM
A PHP Recursive Descent Parser for expression evaluation Merlin Programmer's Cubicle 0 05-30-2009 08:54 PM


All times are GMT. The time now is 06:48 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright ©2003 - 2012 by Unintended Features