I would like to introduce you, the same, primitive game, called: 'Rock Paper Scissors'. Maybe, a little bit improved, in PHP this time.
Pokazywanie postów oznaczonych etykietą PHP. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą PHP. Pokaż wszystkie posty
2013-03-13
2013-03-09
What EOT is.~
If you want to know what EOT is, you need to know first, what 'here-doc' is. Using the simplest words, 'here-doc' (also called 'here document') is the way to delimit strings. It preserves the line breaks as well as other whitespace in the text. Jeffrey Way shows me some nice examples of presenting strings in PHP:
?php>
$info = array(
'name' => 'Caroline';
'surname' => 'Smith';
'gender' => 'Woman';
);
...
// If we have an array with some strings inside, which we would like to present, we can use this form:
...
$result = "<h2>{$info['gender']}</h2><p>name is:</p><h3>{$info['name']}{$info['surname']}</h3>";
echo $result;
...
// Or this form:
...
$result = "<h2>".$info['gender']."</h2>";
$result .= "<p>name is:</p><h3>".$info['name'];
$result .= $info['surname']."</h3>";
echo $result;
...
// Or we can use 'here-docs', which will delimit my strings, this way:
...
extract($info);
$result = <<< EOT
<h2>$gender</h2>
<p>name is:</p>
<h3> $name $surname</h3>
EOT;
?>
The results will be, in every way, the same: Woman name is: Caroline Smith
However, we need to remember, that if we want to EOT work in this case, we need to remember about two things. To put at the beginning of the code, 'extract' function, which will import variables from an array into the current symbol table (so that, we don't need to write in our code, $variable['array_element'] but only concrete variable, that we need, so the code looks cleaner), and at the end of the code, on a new line, we need to put EOT, in the same way, as I wrote above.
2013-03-02
Jsfiddle.net - Test your code there~
My boyfriend show me a great site, called:
Where you can test your code, when you're writing some applications in JavaScript or write comething in CSS and HTML. At the description of the site you can find an identification as: 'mootshell', and it's in 100% right identification, because at the site you can test in a very simple, and what is more important easy way to check your snippets before implementing them. It is also, a good way to put some results of your work, which you normally can not put into your private site, for example on blogger (where only the most simple CSS properties are working), because of a frame ('iframe') with result, which is available on jsfiddle.net and which you can put then, in your HTML code of your private site.
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
}, $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) {return strlen($i);
},$t);
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
}
<?php
switch($variable){
case 'value';
// do something
break;
default:
// do something else
}
?>
Let's write here, piece of code, by using conditional statement 'if':
$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!';
}
?>
<?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':
$day='Thursday';
switch($day) {
case 'Monday':
echo 'Today is Monday!';
break;
case 'Tuesday':
echo 'Today is Tuesday!';
break;
<?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;
echo 'Today is Wednesday!';
break;
case 'Thursday':
echo 'Today is Thursday!';
break;
echo 'Today is Thursday!';
break;
case 'Friday':
echo 'Today is Friday!';
break;
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 ;)
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
}
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!';
}
?>
<?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
2012-11-18
Reflection about Object-Oriented Programming..~
I started to teach PHP language from Wikibooks. Not so ambitious, but I had to start from something. This book realized me one thing – I don’t understand object-oriented programming~
~ I think that this is many young programmists’s problem. A lot of people from my University (IT Technology students) can create very simple or little more difficult programs, but they don’t know anything about obiect-orinted programming at all. So this days, I’m leaving PHP study for a while, to teach myself what obiect-oriented programming really is, because I think that under this concept is hired the real essence of programming not only in PHP but in all else programming languages..~
Subskrybuj:
Posty (Atom)