Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, February 7, 2012

PHP if / else if / else Statement Example

IF Statement

If an expression is TRUE then execute a statement.

if (expression) {
    statement;
}


Example
$a = 2;
$b = 4;

if ($a < $b) {   echo 'a is smaller than b'; }
Result
a is smaller than b
But what if the expression is FALSE?

IF, ELSE Statement

If an expression is TRUE do this, if FALSE do that.

if (expression) {
    statement;
} else {
    statement;
}


Example
$a = 6;
$b = 4;

if ($a < $b) {   echo 'a is smaller than b'; } else {   echo 'a is not smaller than b'; }
Result
a is not smaller than b
What if you want to check more than one expression?

IF, ELSE IF, ELSE Statement

Testing multiple expression if TRUE then execute default statement if FALSE

if (expression) {
  statement;
} elseif (expression) {
  statement;
} else {
  statement;
}


Example
$a = 4;
$b = 4;

if ($a < $b) {   echo 'a is smaller than b'; } else if ($a == $b) {   echo 'a equals b'; } else {   echo 'a is not smaller than b'; }
Result
a equals b

Sunday, December 11, 2011

PHP Numbers

Since we know what numbers are, lets look the following examples how to deal numbers using PHP.

Math Operations

It will echo back 12+4+$value1 and multiplied by $value2 then divided by 2 minus 5
$value1 = 4;
$value2 = 5;

echo ((12 + 4 + $value1) * $value2) / 2 - 5; // result is 45
Using +=, -=, *= and /= will update the value of $price
$price = 8;

echo $price += 8; echo ', '; // new value of $price = 16
echo $price -= 8; echo ', '; // new value of $price = 8
echo $price *= 8; echo ', '; // new value of $price = 64
echo $price /= 8; echo ', '; // new value of $price = 8
Incrementing (go up by 1)
$student = 4;

$student++; echo $student; // result is 5
Decrementing (go down by 1)
$student = 4;

$student--; echo $student; // result is 3

Saturday, December 10, 2011

PHP Strings

A string is series of characters, where a character is the same as a byte, so PHP only supports a 256-character set.

How to use Strings

Single quoted strings
echo 'Hello World';
Double quoted strings
echo "Hello World";
Double quoted strings with variable
echo "$var1 Hello World";
Double quoted strings with variable inside curly braces
echo "{$var1} Hello World";
Concatenating Strings
echo "Hello" . " World";
echo "Hello" . $var1;
echo getvar1() . "Hello";

PHP Variables

As many programming language define, variables is a symbolic representation of a value.

A valid variable name:
  1. Starts with a dollar sign ($)
  2. Followed by either a letter or underscore
  3. Can contain numbers, numbers, underscores, dashes (hyphen)
  4. Case-Sensitive
  5. No spaces
Variable name examples:
$Products;
$salePrice;
$sale_price;
$item3;
$_itemName;
$__itemPrice;
Avoid these variable format:
$_itemPrice; //PHP defined variables, $_POST, $_GET etc.
$__itemPrice; //double/triple/four underscore can be confusing
Lets take Variables in action:
<html>
    <head>
        <title>PHP Variables</title>
    </head>
    <body>
        <?php
        $price1 = 25;
        echo $price1 . '<br />';
        $products = 'product lowercase';
        $Products = 'product uppercase';
        echo $products . '<br />';
        echo $Products . '<br />';
        $price1 = 500;
        echo $price1;
        ?>
    </body>
</html>

Friday, December 9, 2011

PHP Basics

Before proceeding, i suggest to install XAMPP in your computer first if you still don't have server set up for testing and practicing PHP.

How to Embed PHP?

Using PHP tag (standard tag) or PHP block tells the server to execute php.
<?php ?>
PHP shorthand tags or short open tags, this will still work but its considered bad practice.
<? ?>
or
<?= ?>
Create a new php file using your available text editor (Notepad, Notepad++, Komodo Edit, etc.). Name it whatever you like, copy the codes below and save it.
<html>
    <head>
        <title>PHP Embedding</title>
    </head>
    <body>
        <?php echo 'Hello World'; ?><br />
        <?php echo 'Hello' . ' World <br />'; ?>
        <?php echo 18+5; ?>
    </body>
</html>
Open your browser (Internet Explorer, Firefox, Chrome, Opera, etc.) and type this URL http://127.0.0.1/ or http://localhost/ followed by php file name. Example: http://localhost/helloworld.php.

PHP Comments

Because we want to be a good programmer, it is important to use comments into our code so that we're able to quickly understand what the code or function commands especially very large complex programming.

We can make comments in PHP using the following:
<html>
    <head>
        <title>PHP Comments</title>
    </head>
    <body>
        <?php
        // single-line comments
        
        # single-line comments
        
        /* multi line comments
        multi-line comments
        multi-line comments
        multi-line comments
        multi-line comments
        multi-line comments */
        ?>
    </body>
</html>

Tuesday, December 6, 2011

How to Install and Configure XAMPP



What is XAMPP?

XAMPP is an open source or free cross-platform web server package, consist of the following:
  • Apache HTTP Server
  • MySQL database
  • Interpreters for scripts written in the PHP and Perl programming languages


Used to

To allow website designers and programmers to test their work on their own computers without any access to the Internet. XAMPP also provides support for creating and manipulating databases in MySQL using PhpMyAdmin, so it's officially designed for use only as a development tool.



Installation

Using installer version is the easiest way installing XAMPP.


I recommend to install at default settings but if you know what you're doing do so. Click finish.




Configuration

After the installation completed, you will find XAMPP shortcut on your desktop which runs the XAMPP Control Panel to start or stop all server (Apache, MySQL and FileZilla), also to install or uninstall services.


To test XAMPP go to these address http://localhost/ or http://127.0.0.1/ and select your prefer language if prompted. Under the Tools section browse to phpMyAdmin.



Security

By default phpMyAdmin has a username of  root  but no password, it's a good practice to always have your phpMyAdmin protected and secured. To enable MySQL security on XAMPP go to http://localhost/, and browse to Security.


Under the Security page, go to http://127.0.0.1/security/xamppsecurity.php to access Security console MySQL and XAMPP directory protection.


There you go, MySQL databases on PhpMyAdmin are now secured which you can access here, then log in using username as  root (default) and your password.



Where should I put my PHP for testing?

You can save PHP files to this directory "C:\xampp\htdocs\" if you install XAMPP by default or you can locate it by using XAMPP Control Panel Application. Click Explore button and look for htdocs folder.




Sunday, December 4, 2011

PHP Overview


What is PHP?
  • PHP: Hypertext Preprocessor
  • General-purpose Server-side Scripting Language
  • Designed for Web Development (HTML)
  • Add flexibility to HTML
  • Syntax is similar to ASP, C, Java, Perl

Why use PHP?
  • Open Source or Free to use
  • Cross platform (Windows/Mac/Linux)
  • Powerful and Scalable
  • Specifically used for Web Development
  • Can be Object Oriented (ver. 5)
  • Great Documentation (in many languages)
  • Large active developer community like Wordpress, Joomla, PhpBB, etc.

What is needed to practice PHP?