Two simple questions about variables.

These I'm sure are quickies.

1.  I have a var read from a file "0001".  I need to increment it by 1 and have the result "0" filled.  If I move the row['data'] to a work field and add 1, it becomes numeric.

2.  I have a var from a file "G".  The range of this var can be from "A" to "P".  I need to increment or decrement it by 1 (minus = "F" plus = "H").  What's the easiest way to do this?
breeze351Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
#1.  Yes it does.  PHP 'helps' you when you do arithmetic on a text field, it automatically converts it to a number.  To convert it back to a formatted text, you need to use 'sprintf()' http://php.net/manual/en/function.sprintf.php .  
$newnum = sprintf("%4d", $oldnum);

Open in new window


#2. Use 'ord()' to get the numeric value of the character and 'chr()' to convert it back after adding or subtracting.  http://php.net/manual/en/function.chr.php
Ray PaseurCommented:
That's really two questions.

To the first, you can use sprintf() or str_pad() to convert the integer value to the padded string.  

PHP uses loose data typing and type coercion in assignment statements, comparisons, arithmetic, etc.  This is one of the worst and best features of PHP - at the same time.  You have to understand what it is doing to your data if you're going to write PHP programs.  Here are the links you want to read and understand thoroughly.
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/en/types.comparisons.php

To the second, I would just make an array of characters and use numeric indexing to point at the letter you want.  Some examples are in the second part of the code snippet.
http://iconoun.com/demo/temp_breeze351.php
<?php // demo/temp_breeze351.php

/**
 * http://www.experts-exchange.com/questions/28709614/Two-simple-questions-about-variables.html
 */

// RAISE THE ERROR REPORTING LEVEL TO THE HIGHEST POSSIBLE SETTING
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';


/**
 * FIRST QUESTION - ADD BUT RECOVER STRING PADDING WITH LEADING ZEROS
 */
$val = '0001';
var_dump($val);

// ADD A VALUE TO THE ORIGINAL
$new = $val + 1;

// SHOW INTERMEDIATE VALUE AFTER ARITHMETIC
var_dump($new);

// CORRECT THE FORMATTING AFTER PHP LOOSE DATA TYPING
$new = str_pad($new, 4, '0', STR_PAD_LEFT);
var_dump($new);


/**
 * PACKAGE THE ZERO-PADDED ARITHMETIC AS A FUNCTION
 */
function zero_padded_arithmetic($val, $incr, $lng=4)
{
    return str_pad($val + $incr, $lng, '0', STR_PAD_LEFT);
}

// TEST THE FUNCTION
var_dump(zero_padded_arithmetic('0001', '1'));
var_dump(zero_padded_arithmetic('003',  '-3'));



/**
 * SECOND QUESTION - AN ARRAY OF LETTERS, INDEXED WITH A CORRESPONDING ARRAY OF NUMBERS
 */
$letters = range('A', 'P');
$numbers = array_flip($letters);

// CHOOSE LETTER 'G'
$old = 'G';
var_dump($old);

// ADD OR SUBTRACT A VALUE
$key = $numbers[$old] + 1;
$new = $letters[$key];
var_dump($new);

$key = $numbers[$old] - 1;
$new = $letters[$key];
var_dump($new);


/**
 * PACKAGE THE ARRAY LETTER ARITHMETIC AS A FUNCTION
 */
function alphabet_arithmetic($letter='A', $increment='0', $range=array('A', 'Z'))
{
    $letters = range($range[0], $range[1]);
    $numbers = array_flip($letters);
    $key     = $numbers[$letter] + $increment;
    $retval  = isset($letters[$key]) ? $letters[$key] : FALSE;
    if (!$retval) trigger_error("$letter + $increment is out of range", E_USER_WARNING);

    return $retval;
}

// TEST THE FUNCTION
var_dump(alphabet_arithmetic());
var_dump(alphabet_arithmetic('G', +1));
var_dump(alphabet_arithmetic('C', -1));
var_dump(alphabet_arithmetic('C', -2));
var_dump(alphabet_arithmetic('C', 23));
var_dump(alphabet_arithmetic('G', -1, ['A', 'P']));

var_dump(alphabet_arithmetic('C', -3));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
breeze351Author Commented:
Thanks.  That puts me going in the right direction.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.