Rabu, 04 Mei 2011

Superglobals

As a general rule, PHP does not support global variables (variables that can automatically be accessed from any scope). However, certain special internal variables behave like global variables similar to other languages. These variables are called superglobals and are predefined by PHP for you to use. Some examples of these superglobals are<span class="fullpost">
  • $_GET[]. An array that includes all the GET variables that PHP received from the client browser.
  • $_POST[]. An array that includes all the POST variables that PHP received from the client browser.
  • $_COOKIE[]. An array that includes all the cookies that PHP received from the client browser.
  • $_ENV[]. An array with the environment variables.
  • $_SERVER[]. An array with the values of the web-server variables.
These superglobals and others are detailed in Chapter 5, "How to Write a Web Application with PHP." On a language level, it is important to know that you can access these variables anywhere in your script whether function, method, or global scope. You don't have to use the $GLOBALS[] array, which allows for accessing global variables without having to predeclare them or using the deprecated globals keyword.</span>

empty()

empty() may be used to check if a variable has not been declared or its value is false. This language construct is usually used to check if a form variable has not been sent or does not contain data. When checking a variable's truth value, its value is first converted to a Boolean according to the rules in the following section, and then it is checked for TRue/false.<span class="fullpost">
For example:
if (empty($name)) {
    print 'Error: Forgot to specify a value for $name';
}

This code prints an error message if $name doesn't contain a value that evaluates to true.</span>

unset()

unset() "undeclares" a previously set variable, and frees any memory that was used by it if no other variable references its value. A call to isset() on a variable that has been unset() returns false.
For example:
$name = "John Doe";<span class="fullpost">
unset($name);
if (isset($name)) {
    print '$name is set';
}

This example will not generate any output, because isset() returns false.
unset() can also be used on array elements and object properties similar to isset().</span>

Managing Variables

Three language constructs are used to manage variables. They enable you to check if certain variables exist, remove variables, and check variables' truth values.

<span class="fullpost">
isset() determines whether a certain variable has already been declared by PHP. It returns a boolean value true if the variable has already been set, and false otherwise, or if the variable is set to the value NULL. Consider the following script:
if (isset($first_name)) {
    print '$first_name is set';
}

This code snippet checks whether the variable $first_name is defined. If $first_name is defined, isset() returns true, which will display '$first_name is set.' If it isn't, no output is generated.
isset() can also be used on array elements (discussed in a later section) and object properties. Here are examples for the relevant syntax, which you can refer to later:
  • Checking an array element:
    if (isset($arr["offset"])) {
        ...
    }

  • Checking an object property:
    if (isset($obj->property)) {
        ...
    }
Note that in both examples, we didn't check if $arr or $obj are set (before we checked the offset or property, respectively). The isset() construct returns false automatically if they are not set.
isset() is the only one of the three language constructs that accepts an arbitrary amount of parameters. Its accurate prototype is as follows:
isset($var1, $var2, $var3, ...);

It only returns TRue if all the variables have been defined; otherwise, it returns false. This is useful when you want to check if the required input variables for your script have really been sent by the client, saving you a series of single isset() checks.</span>

Indirect References to Variables

An extremely useful feature of PHP is that you can access variables by using indirect references, or to put it simply, you can create and access variables by name at runtime.
Consider the following example:
$name = "John";
$$name = "Registered user";
print $John;<span class="fullpost">

This code results in the printing of "Registered user."
The bold line uses an additional $ to access the variable with name specified by the value of $name ("John") and changing its value to "Registered user". Therefore, a variable called $John is created.
You can use as many levels of indirections as you want by adding additional $ signs in front of a variable.</span>

Variables

Variables in PHP are quite different from compiled languages such as C and Java. This is because their weakly typed nature, which in short means you don't need to declare variables before using them, you don't need to declare their type and, as a result, a variable can change the type of its value as much as you want.<span class="fullpost">
Variables in PHP are preceded with a $ sign, and similar to most modern languages, they can start with a letter (A-Za-z) or _ (underscore) and can then contain as many alphanumeric characters and underscores as you like.
Examples of legal variable names include
$count
$_Obj
$A123

Example of illegal variable names include
$123
$*ABC

As previously mentioned, you don't need to declare variables or their type before using them in PHP. The following code example uses variables:
$PI = 3.14;
$radius = 5;
$circumference = $PI * 2 * $radius; // Circumference = p * d

You can see that none of the variables are declared before they are used. Also, the fact that $PI is a floating-point number, and $radius (an integer) is not declared before they are initialized.
PHP does not support global variables like many other programming languages (except for some special pre-defined variables, which we discuss later). Variables are local to their scope, and if created in a function, they are only available for the lifetime of the function. Variables that are created in the main script (not within a function) aren't global variables; you cannot see them inside functions, but you can access them by using a special array $GLOBALS[], using the variable's name as the string offset. The previous example can be rewritten the following way:
$PI = 3.14;
$radius = 5;
$circumference = $GLOBALS["PI"] * 2 * $GLOBALS["radius"]; // Circumference = p * d

You might have realized that even though all this code is in the main scope (we didn't make use of functions), you are still free to use $GLOBALS[], although in this case, it gives you no advantage.</span>

Comments

The next thing you need to learn about PHP is how to write comments, because most of the examples of this chapter have comments in them. You can write comments three different ways:
  • C way
    /*  This is a C like comment
     *  which can span multiple
     *  lines until the closing tags
     */
    <span class="fullpost">
  • C++ way
    // This is a C++ like comment which ends at the end of the line

  • Shell way
    # This is a shell like comment which ends at the end of the line</span>

HTML Embedding

The first thing you need to learn about PHP is how it is embedded in HTML:
<HTML>
<HEAD>Sample PHP Script</HEAD>
<BODY>
The following prints "Hello, World":<span class="fullpost">
<?php

    print "Hello, World";

?>
</BODY>
</HTML>

In this example, you see that your PHP code sits embedded in your HTML. Every time the PHP interpreter reaches a PHP open tag <?php, it runs the enclosed code up to the delimiting ?> marker. PHP then replaces that PHP code with its output (if there is any) while any non-PHP text (such as HTML) is passed through as-is to the web client. Thus, running the mentioned script would lead to the following output:
<HTML>
<HEAD>Sample PHP Script</HEAD>
<BODY>
The following prints "Hello, World":
Hello, World
</BODY>
</HTML>

Tip
You may also use a shorter <? as the PHP open tag if you enable the short_open_tags INI option; however, this usage is not recommended and is therefore off by default.
Because the next three chapters deal with language features, the examples are usually not enclosed inside PHP open and close tags. If you want to run them successfully, you need to add them by yourself.</span>

Creating the Tip of the Day Program

Way back at the beginning of this chapter, I promised that you would be able to
write the featured Tip of the Day program. This program requires HTML, CSS, and
one line of PHP code. The code shows a reasonably basic page:<span class="fullpost">
<html>
<head>
<title>Tip of the day</title>
</head>
<body>
<center>
TRICK
TRAP
TRICK
<h1>Tip of the day</h1>
<div style = “border-color:green; border-style:groove; border-width:2px”>
<?
readfile(“tips.txt”);
?>
</div>
</center>
</body>
</html>
The page is basic HTML. It contains one div element with a custom style setting
up a border around the day’s tip. Inside the div element, I added PHP code with
the <? and ?> devices. This code calls one PHP function called readFile(). The
readFile() command takes as an argument the name of some file. It reads that
file’s contents and displays them on the page as if it were HTML. As soon as that
line of code stops executing (the text in the tips.txt file has been printed to the
Web browser), the ?> symbol indicates that the PHP coding is finished and the
rest of the page will be typical HTML.</span>

Selasa, 03 Mei 2011

Cara Membuat Koneksi Database MySQL dengan PHP

"OM Swastyastu"
Mungkin sudah banyak ada artikel mengenai pembahasan mengkoneksikan PHP dengan MySQL,nah disini saya ingin sedikit berbagi membuat script PHP untuk koneksi ke database MySQL. langsung aja ketik script dibawah ini
<?php
mysql_connect("localhost","root","") or die ("not conect with server");
mysql_select_db ("dbname") or die ("not connect with database");
?>

mungkin sekian dulu dari saya.lanjut nanti pada artikel berikutnya

OM Santi Santi Santi OM