php

string bassAckwards ( string $input )
Write a PHP function ‘bassAckwards’ that will take a comma delimited string such as:
Php,Cookies,Arrays,Mysql,LAMP

And will return a string with the individual elements reversed, but in the same order:
phP,seikooC,syarrA,lqsyM,PMAL

I am being given this question and i have no prior knowledge of php i would like an explanation of how to go about figuring this out

Since I don’t know if you have any experience programming at all, I am going to expect we are starting from scratch here.

First of all, some expressions you will need to know.

String

Function
A block of code which you can refer to in your code. This helps us to keep good coding standards, like DRY (Don’t Repeat Yourself). Instead of writing a portion of code multiple times to the code, you can just run the function whenever you need to.

Array
An array is basically a collection of data, you could envision a table.

[hr]

That should basically cover the basic knowledge needed for this task. Let’s start breaking down your text.

Ok! This is easy.

[php]<?php
// you should always start execution of PHP with <?php

/*

  • This is our function, pretty easy to define, defined like this you have to supply a variable to the function.
  • Note that you do not have to name the variable “$string”, the first variable the function recieves will be interpreted as “$string” in the scope of the function.
    /
    function bassAckwards ($string) {
    /
    This is inside the function scope.
    • Here we have no access to outside variables (except globals)
    • Nothing here yet
      */
      }[/php]

[hr]

Now it’s getting more challenging, but nothing we can’t handle.

So to break it down even further.

Input string

Php,Cookies,Arrays,Mysql,LAMP

We have to break this up somehow, into a list (array):

Array ( [0] => Php [1] => Cookies [2] => Arrays [3] => Mysql [4] => LAMP )

Then we can easily iterate trough the code, and mirror every word, into something like this:

Array ( [0] => phP [1] => seikooC [2] => syarrA [3] => lqsyM [4] => PMAL )

Then we can convert it back to a comma-delimited string:

phP,seikooC,syarrA,lqsyM,PMAL

[hr]

This basically looks like this:
[php]<?php

function bassAckwards ($string) {
// first we break up the string into an array
$array = explode(’,’, $string);

// Let's create a variable called result to hold the modified data.
$result = array();

// I like foreach because it's quite literal, for each array entry we get a variable $word. The code inside the curly brackets will run as many times as there are array entries, if we don't break out (which we don't do in this case).
foreach ($array as $word) {
  // with the square brackets we assign the value into a new array entry
  $result[] = strrev($word);
}

// return the $result array, oh, and convert it back to a comma-delimited string first
return implode(',', $result);

}[/php]

[hr]

Example code:

[php]<?php

function bassAckwards ($string) {
$array = explode(’,’, $string);

$result = array();
foreach ($array as $word) {
  $result[] = strrev($word);
}
return implode(',', $result);

}

$string = ‘Php,Cookies,Arrays,Mysql,LAMP’;
$newString = bassAckwards($string);

echo 'Before: ’ . $string . ‘
’;
echo 'After: ’ . $newString;[/php]

[hr]

Output:

Before: Php,Cookies,Arrays,Mysql,LAMP After: phP,seikooC,syarrA,lqsyM,PMAL

Sponsor our Newsletter | Privacy Policy | Terms of Service