Tag: php

Learning PHP – Step 4 – Statements

In my last post I’ve talked about variables and operators. Now, lets see what can we do with them.

Statement: if

The “if” statement has 3 main forms:



   
       
        Learning PHP – Step 2
   
   
                 $name = "José Cruz";
         $even = 10 % 2;
         $odd = 10 % 3;
         $someValue = $even * 4;
         $ifvar = 1;
        
         // it’s even – simple "if"
         if($even == 0) {
             echo "10 its even!";
         }

         // it’s even – simple "if" with an else
         if($even == 0) {
             echo "10 its even!";
         } else {
             echo "10 its odd! Really?";
         }
        
         // it’s even – complex "if"
         if($even == 0) {
             echo "10 its even!";
         } elseif($ifvar == 1) {
             echo "Althougth true, the first branch has already taken the flow! :(";
         } elseif($ifvar == 2) {
             echo "Just to ensure you understand this construction!";
         } else {
             echo "10 its odd! Really?";
         }
        ?>
   

 

Statement: switch

“switch” it’s a variation of “if”, but for cases where we have some kind of a list of values belonging to a common domain, like: weekdays, months, (1,3,4,5), etc.

The expression inside “switch()” its evaluated and the result will be compared with each of the “case ”. Each “case” can have the “break” statement, which terminates the statement. If its missing, the execution flow goes to the very next “case” (if its valid) until a “break” it’s found.

If a “case ” was not found, then the “default” its used (it it exists).



   
       
        Learning PHP – Step 2
   
   
                $weekday = 1; // assuming a week starts on sunday with value 1
       
        switch($weekday) {
            case 1:
                echo "Sunday";
                break;
            case 2:
                echo "Monday";
                break;
            case 3:
                echo "Tuesday";
                break;
            case 4:
                echo "Wednesday";
                break;
            case 5:
                echo "Thursday";
                break;
            case 6:
                echo "Friday";
                break;
            case 7:
                echo "Saturday";
                break;
            default:
                echo "big mistake1";
        }
        
        
        ?>
   

 

And that’s it!

Next post about loops!

 

Technorati Tags: ,,,,,,,
Windows Live Tags: programming,else,switch,if,php,Statements,expression,break
WordPress Tags: programming,else,switch,if,php,Statements,expression,break

Learning PHP – Step 3 Basics

Following previous posts, lets start with the basics.

Variables:

– Always start with $ and can have the following chars _ [a-z] [A-Z] and [0-9]. the first symbol must be an _ ou a letter.

$count = 0;
$name = “José Cruz”

 

What about types? If you know python, ruby or the “var x = 0;” construction from C# you know how PHP works. It converts the variable for the type defined by the value initialized. For instance: $count = 0; will be an “integer”.

Lets use some variables in our script:



   
       
        Learning PHP – Step 2
   
   
                $name = "José Cruz";
         echo $name . " is starting to understand PHP!";
        ?>
   

If you run the script above it will display “José Cruz is starting to understand PHP!”

PS: The dot between $name and “ is starting… ” is the concatenation in strings. This is new. Other languages usually use the plus sign or some function to do it.

The operators are the same as C or mainstream languages:

Arithmetic: +, ++, –, –, *, /, %

Assignment: =, +=, –=, *=, /=, %=, .=

Comparasion: ==, <=, <, >= >, <>, !=

Logic: &&, ||, !

PS: You can use “(“ and “)”

Some examples:

 

$a = 5 + 2 * (7 + 5);
$a++;
$b = $a –4;
$c = $a / $b;
$even = 10 % 2;
$a+=7; // same as $a = $a + 7;
$even = 10;
$even %= 2;
$verytrue = 2 < 7;
$veryfalse = 6 >= 3;
if( $a > 3 ) …. // future post we talk about if and other statements.
if( $a > 3 && $b < 10 || $even == 0 ) …

 

As with other languages:

– Unary operators precede all other ones ( “!” )

– then: *, / and  &&

– Last: +, –, ||

 

Lets try this in our php website:



   
       
        Learning PHP – Step 2
   
   
                 $name = "José Cruz";
         $even = 10 % 2;
         $odd = 10 % 3;
         $someValue = $even * 4;
        
         echo $name . " is starting to understand PHP with some operators" . "
";
         echo "even values: ". $even . "
";
         echo "odd values: ". $odd . "
";
         echo "some value = " + $someValue . "
";
        ?>
   

 

Run it!

Next post: statements.

Technorati Tags: ,,,,
WordPress Tags: language,programming,Variables,Basics,PHP

Learning PHP – Step 2

Following my previous post I’m now preparing an environment to develop in PHP.

1) Install XAMPP – There are other alternatives, but this one, after some research was my choice. Why? Because in the future I’m thinking developing some skills in Perl and with XAMPP it’s a 2 in 1! (Besides mysql included!)

2) IDE – I’m trying Netbeans 7.1. There are a lot others IDEs out there like: komodo, phpstorm, phpide, aptana, eclipse, phpdesigner. My choice probably it’s not the best one, but I develop in Visual Studio and Eclipse and as a Oracle trainer, sometimes I have to use Netbeans. I will try it, if the experience gets troubled I will change to another and keep this learning. I think, anyway, the experience will be always positive.

I’m now following the steps to create a project in Netbeans…

  • New Project PHP
  • Advantage – Netbeans detected my xampp instalation and discovered where to put the php files.
  • Project name: LearningPHP (in my drive sits at: D:\xampp\htdocs\LearningPHP)
  • Local Web Site
  • Project URL: http://localhost:8001/LearningPHP/
  • I will not use any PHP framework… maybe in a future post I’ll try one besides WordPress.

After the wizard I have the file below already created:

index.php (the first file in any web tech is always index.



   
       
       
   
   
                // put your code here
        ?>
   

 

Now I will change the Title and put some basic code inside the



   
       
        Learning PHP – Step 2
   
   
                 echo "My second step to learn php!";
        ?>
   

 

Let’s run it (In Netbeans just hit the “play”!

PS: Make sure xampp is running and the port it’s the correct one. In my example the url is: http://localhost:8001/LearningPHP/index.php

 

and the result, as expected, it’s the browser output:

My second step to learn php!

 

Now, I have an environment to run php and a first “hello word” example.

Next post: The usual language basics of php!

Learning PHP – Step 1

 

I develop in several programming languages like C#, VB.NET, VB, Java, JavaScript, Python, Groovy, Powershell, PL-SQL, T-SQL, a little of Ruby, C, C++, Assembly (in the last years not so often), Prolog, List/Scheme, F#, Perl and and few others less used. I’ve read and I can understand PHP, from the knowledge I have from “C” Languages, but I never developed a complete project in PHP, and I don’t know if in the future I will need to do it, but from what I’ve been reading PHP is used in the TOP CMS Frameworks/Applications as WordPress, Drupal, Joomla, etc.

I’m thinking to create a synergy here, I will deep my understanding and knowledge about PHP and WordPress, tweaking my blog, this blog! Which I think will enrich my CV and, in the future, my career, making me a better team leader and software architect.

Starting PHP.

1) It’s a language to use in web sites.

2) You mix HTML with PHP, which is different from ASP.NET and JSP for instance. To differentiate the HTML from PHP you use in the middle of the HTML. The server will know how to process the PHP and merge it with HTML.



My first example in PHP

 

This code will output “My first PHP!” in the browser.

Great! It’s the first step!

Next Post let’s install the software we need to run this example. Then, the sky it’s the limit!

Bad Behavior has blocked 162 access attempts in the last 7 days.

Hyper Smash