2013-02-21

The array_map() problems!~..

Some time ago, I had a huge problem with understanding the array_map () function. Jeffrey Way defines it as a PHP function, which is built from 2 parameters. The first is the function to execute, and the second parameter is the array, are you working with. First parameter, within array_map, executes on every item in our parameter 2, in the array. So, array_map() syntax looks like:

        array_map(function() {
                    }, array1,array2,array3,...) // required is only array1, the remaining arrays are optional

He also gave me some example of use:

     
       function array_pluck($toPluck, $arr) {
                  $ret= array_map(function($item) use ($toPluck){
                  return $item[$toPluck];
                  }, $arr);
       print_r($ret);
       }
      ?>

But, as on my first contact with this function, the example was too much complicated for me. Or maybe I could understand it just because of 'use' enclosure, which can not be used in older versions of PHP. But whether to this, I asked my boyfriend to help me, and after his lesson, I finally understood, how array_map() working. There is his example:

      
       $table = [1,2,3] // so we have an array called $table
       $new_table = array_map(function($item) { // we create a new array, equal to array_map
             return $item+1;
             }, $table); // The function within array_map(), is working on every item from the $table and for each item, adds one
       print_r($new table); //now let's print it and see the result
       ?>



RESULT:

Array(
                  [0] => 2
                  [1] => 3
                   [2] => 4)

So, as you can see, the array_map() function works on array which as components, contains other arrays with other elements. Array_map() applying to array, execute function to each one and after that, returns an array containing all this elements, that are the execute function result.

Of course, if you want to see the result of array_map(), you don't have to use print_r, which gives you a lot of, unnecessary details. You can see the result, by using nicer way, namely, using 'foreach' conditional statement:

      

        $t = ['mama', 't', 'xx'];
        $r = array_map(function($i){
               return strlen($i);
               },$t);
        foreach($r as $item) {
               echo $item.' ';
        }
        ?>

RESULT:   4 1 2

2013-02-16

Have you heard about 'Sscanf' function?

When I asked my boyfriend, when the function 'Sscanf' can be useful, he couldn't give me a good answer to this question. My boyfriend is a PHP programmer..
.. The fact is that this function is not well known because most of the complex codes can be written without the knowledge of the 'Sscanf'. But if such function exists in the PHP programming language, there's always the possibility that, when we will be reading someone's code, we can just come across at it. Therefore, it is good to know, what 'Sscanf' is. The syntax of the function looks like this:

                                 sscanf(string, format, arg1, arg2, arg3..)

 'Sscanf' function works very easy. It is essentially the inverse of 'Sprintf' function. The 'Sprintf' syntax looks like this:

                                 sprintf(format,arg1,arg2,arg3..)

'Sprintf' function writes a formatted string to a variable. While 'Sscanf' function parses input from a string according to a format, which means that 'Sscanf' reads input for numbers and other data types from standard input. We will understand it better on simple example:

                                 <?php
                                 $result1= sprintf("s% s%, d%",'January','1st','2013');
                                 $result2= sscanf("January 1st, 2013", 's%','s%','d%');

                                 echo $result1;
                                 print_r($result2);
                                 ?> 
RESULT1:                 RESULT2:
 January 1st, 2013                    Array(    
                                                           [0]=>January
                                                    [1]=>1st,
                                                       [2]=>2013)

So as you can see, we can use 'Sscanf' when we want to capture specific values with the variables. But of course if I'm wrong, I'm always open for correction.

2013-02-14

'If' and 'switch' as Convertible Conditionals!~

In a previous post, I explained how the conditional statement 'if' syntax looks like. It is very useful when we are writing different programs, but sometimes, when the code is more complicated, it starts to look like a very messy code. Therefore, we can alternatively use convertible conditional statement 'switch', whose syntax I wrote below:

                                                 <?php

                                                 switch($variable){
                                                           case 'value';
                                                                     // do something
                                                                     break;
                                                           default:
                                                                    // do something else
                                                 }
                                                 ?>

Let's write here, piece of code, by using conditional statement 'if': 

                                                 <?php
 
                                                 $day='Thursday';

                                                 if($day=='Monday') {
                                                          echo 'Today is Monday!';
                                                 } elseif($day=='Tuesday') {

                                                          echo 'Today is Tuesday!'; 
                                                 } elseif($day=='Wednesday') {
                                                          echo 'Today is Wednesday!'; 
                                                 } elseif($day=='Thursday') {
                                                          echo 'Today is Thursday!'; 
                                                 } elseif($day=='Friday') {
                                                          echo 'Today is Friday!';
                                                 } else {
                                                          echo 'Today is Weekend!';
                                                 }
                                                 ?>

As soon as I wrote here something by using statement 'if', you can see that statement 'switch' can be use in the same way and this piece of code that I wrote you, I can write in a different way, by using the conditional 'switch':


                                                 <?php
 
                                                 $day='Thursday';

                                                 switch($day) {    

                                                        case 'Monday':
                                                                     echo 'Today is Monday!'; 
                                                                     break; 
                                                        case 'Tuesday':
                                                                     echo 'Today is Tuesday!'; 
                                                                     break; 
                                                        case 'Wednesday':
                                                                     echo 'Today is Wednesday!'; 
                                                                     break;  
                                                        case 'Thursday':
                                                                     echo 'Today is Thursday!'; 
                                                                     break; 
                                                        case 'Friday':
                                                                     echo 'Today is Friday!'; 
                                                                     break; 
                                                        default:
                                                                     echo 'Today is Weekend!'; 
                                                   }
                                                   ?>

For sure, you can see, the differences between 'if' and 'switch' conditional statements. Although 'switch' requires to be break after every instruction, but on the other hand, it needs only one bracket, due to which the code looks much more better ;)

2013-02-10

PHP has got triple equals!

It's a very interesting question, that in PHP, there are three different use of the '=' sign, in PHP conditional statements. The first and most intuitive, you can simply write, by using the easiest conditional statement - 'if', I think. This is the syntax for statement 'if':

                                                 <?php

                                                 if() {
                                                       // do something
                                                 } else {
                                                      //do something else
                                                 }

                                                 ?>


So we have here the syntax. Now, let's try to write something in PHP. For example:
                                                 <?php
                                                 $day='Monday';

                                                 if($day=='Monday') {
                                                          echo 'Day equal Monday!';
                                                 } elseif($day==='123') {
                                                          echo 'Type and Values are the same!';
                                                 } else {
                                                          echo 'Nothing here match!';
                                                 }
                                                 ?>

As you can see, we used here sign 5 times! The first use of a single character '=', means that we are signing the value to the variable ($day='Monday';). We are setting day equal Monday. At the second case, we are using a double equals ($day=='Monday';), because we are running a test: Does day equal Monday? So we are not signing anything to the variable but we are testing whether the left side equal the right side. Few programmers know, that there is also triple equals ($day==='Monday';). If we are using it, we are whether two variables are equal and also the same type. In other words, it is a comparison to see if they are both integers or both are strings. So cute<3


2013-02-07

Refleksja Schizofrenika..~

Naprawdę muszę coś z tym zrobić. Doszło już do tego stopnia, że w stresujący dzień (myśl: np. w dzień egzaminu) nie mogę wyjść z domu, bez załatwienia pewnych specyficznych, dla tego okresu czynności. Stres miota mną jak szatan! Przecież nie po to ryjesz całą noc, żeby potem (na skutek stresu) wszystko spieprzyć-,- Kampanię anty-stresową uważam za rozpoczętą.