Tuesday, January 5, 2010

Smart Language Switch



As you can see, it is a complex php script.
For basic it will check if there is a ?/&lang set and will search for the lang file in the lang/ dir.
If you want to change the language and click on, for example, a flag it will take the whole url (example: http://test.com/?page=news ) and will add a ?/&lang setting.
But as you can see there can happend a error like: http://test.com/?page=news?lang=de
That is wrong because there are to times a var with "?", so in the script is a detection if something is already set wit "?".

Saturday, December 26, 2009

Algorithm and flow-charts

Okay, I'm happy to write my first entry in this blog, and my first post in english language. First, after talk about programming i want to talk about algorithms.

In the normal life we are constantly subject to solve problems, algorithms are the methods that we use to solve these problems. For example a problem may be "How to cook cake" the recipe is the algorithm.

The algorithm is very used by developers, remember that the computer was born to solve problems. Initially these problems were only mathematical now there are software in all fields: medical, school etc... to represent a solution of a problem was born the flow-chart, this is the most used method: it is made up from blocks wich represent the steps that lead to the resolution of the problem. Each block can have a different form that represent the type of instruction: typical and most used form are four:

  • Oval: the start or the end of the algorithm.
  • Rectangle: an single instruction.
  • Parallelogram: an operation of input or output.
  • The rumble: a binary selection (True or False).

There are others symbols but these are the most used.

Example of algorithm and relative flow-chart and source code (PHP and C++): the linear equation.




C++

int main() {a=2; b=3;

if (a==0) {

if (b==0) {

cout << "Every x is a solution";

} else {

cout << "No solution";

}

} else {

x=-b/a;

cout << "X={" <<>

}

PHP

$a=2; $b=3;

if (a=="0") {

if (b=="0") {

echo "Every X is a solution";

} else {

echo "No solution";

}

} else {

$x=-$b/$a;

echo $x;

}

Friday, December 25, 2009

PHP: Second Steps

Ok let's start with the advanced stuff.

First of all we will talk about the if().
It is really important in PHP and you will often use it. Ok example:

<?php
$var1 = '1';
if ($var1 == '1') {
echo "true";
}
?>


So you see, the if() will check if $var1 contains '1'. If this is true it will print "true" and if it's not true it will do nothing.
You should understand the script completely now.

<?php
$var1 = '0';
if ($var1 == '1') {
echo "true";
} else {
echo "false";
}
?>


Take a look. This script is almost the same like the one above, but now it will do something when $var1 doesn't contain '1'. Ok you know now the basic if(). Let's go on...

<?php
$var1 = '2';
if ($var1 == '1') {
echo "it is 1";
} elseif ($var1 == '2') {
echo "it is 2";
}
?>


The 'elseif ()' is just a "upgraded else ;)
elseif() will check again if the following is true. So you can do almost the same with elseif then you can do with switch:

<?php
$var1 = '2';
switch ($var1) {
case 1:
echo "case 1 is true";
break;
case 2:
echo "case 2 is true";
break;
}
?>


Just read that script carefuly and you will know what it will do.
case 1 for example is doing the same like: if ($var1 == '1') { ... }
I hope you understand now.

Ok next one the while().
while() is doing something until the stuff in (..) is getting false. An example:

<?php
$var1 = '0';
while ($var1 <= '4') {
echo $var1;
$var1++;
}
?>


This script will count to 4.
To understand the script, you must know that $var1++; will do $var1 + 1, so from '0' to '1' and so on. When the $var1 contains '5' the while() will stop because it is not less or equal to '4'.

The rest of this tutorial will follow soon....