Thursday, December 5, 2013

How to use in_array() function in PHP

To check a variable exists in array you can use in_array() function:
Syntax: bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Parameters

needle
The searched value.
Note:
If needle is a string, the comparison is done in a case-sensitive manner.
haystack
The array.
strict
If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

Return Values

Returns TRUE if needle is found in the array, FALSE otherwise.   
Example #1:
<?php
$fruit 
= array("apple""banana""orange");
if (
in_array("apple"$fruit)) {
    echo 
"apple exist in $fruit array";
}
if (!
in_array("iphone"$fruit)) {
    echo 
"iphone not exist in $fruit array";
}
?>
Example #2:
<?php
$fruit 
= array("1""2""4","7");
if (
in_array("1"$fruit)) {
    echo 
"Yes";
}
else
{
    echo "No";
}
?>
Example check array in array:
<?php
$a 
= array(array('a''e'), array('c''b'), 'd');

if (
in_array(array('a''e'), $a)) {
    echo 
"Found";
}
else
{
    echo "Not found";
}
if (
in_array(array('f''i'), $a)) {
    echo 
"Yes";
}

else
{
    echo "No";
}
if (in_array('d'$a)) {
    echo 
"'d' was found";
}
?>

Saturday, November 30, 2013

How to use str_replace function

Define:
This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

Syntax:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Parameters:
If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though.

If search or replace are arrays, their elements are processed first to last.

$search parameter: The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
replace

The replacement value that replaces found search values. An array may be used to designate multiple replacements.
subject

The string or array being searched and replaced on, otherwise known as the haystack.

If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.
count

If passed, this will be set to the number of replacements performed.

Return Values
This function returns a string or an array with the replaced values.

# Example 1: 
<?php// Provides: Welcome to PHP.net. You are learning "How to use str_replace function" lession.$string  "Welcome to PHP.net. You are learning "How to use str_replace function" lession.";

$search 
= array("PHP.net", "Welcome to");
 
$replace = array("PHP tutorial example""Hello");
$new_string str_replace($search$replace$string);
echo 
$new_string; //output: 
//Hello PHP tutorial example. You are learning "How to use str_replace function" lession.
?>
#Example 2
To collapse multiple consecutive space characters to a single one, don't use str_replace() inside a loop--use preg_replace() instead for clarity and better performance:

<?php

$str
= ' This is    a    test   ';
$str = preg_replace('/ +/', ' ', $str);

?>
 
 
#Example 3: Examples of potential str_replace() gotchas
<?php// Order of replacement$str     "Line 1\nLine 2\rLine 3\r\nLine 4\n";$order   = array("\r\n""\n""\r");$replace '<br />';
// Processes \r\n's first so they aren't converted twice.$newstr str_replace($order$replace$str);
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search  = array('A''B''C''D''E');$replace = array('B''C''D''E''F');$subject 'A';
echo 
str_replace($search$replace$subject);
// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a''p');$fruit   = array('apple''pear');$text    'a p';$output  str_replace($letters$fruit$text);
echo 
$output;?>
 

PHP String and diffirent between single quotes and double quotes

PHP - String:
In the last lesson, PHP Echo, we used strings a bit, but didn't talk about them in depth. Throughout your PHP career you will be using strings a great deal, so it is important to have a basic understanding of PHP strings.

PHP - String create double quotes:


Thus far we have created strings using double-quotes, but it is just as correct to create a string using single-quotes, otherwise known as apostrophes.

PHP Code:

$my_string = 'PHP tutorial example - Welcome the first php aplication!';
echo 'PHP tutorial example - Hello PHP.net!';
echo $my_string;

If you want to use a single-quote within the string you have to escape the single-quote with a backslash \ . Like this: \' !
echo 'PHP tutorial example- It\'s Neat!';

PHP - String create double quotes:

PHP Code:
$my_string = "Beginer - PHP tutorial example!";
echo "Developer - PHP employment";
echo $my_string;


We have used double-quotes and will continue to use them as the primary method for forming strings. Double-quotes allow for many special escaped characters to be used that you cannot do with a single-quote string. Once again, a backslash is used to escape a character.

Note: If you try to escape a character that doesn't need to be, such as an apostrophe, then the backslash will show up when you output the string.

These escaped characters are not very useful for outputting to a web page because HTML ignore extra white space. A tab, newline, and carriage return are all examples of extra (ignorable) white space. However, when writing to a file that may be read by human eyes these escaped characters are a valuable tool!

PHP - String create heredoc
PHP Code:
$my_string = <<<TESTOne of the limitations of strpos is that it only returns the position of the very first match. If there are 5,000 other matches in the string you would be none the wiser, unless you take action!
TEST;

echo $my_string;

Display:
One of the limitations of strpos is that it only returns the position of the very first match. If there are 5,000 other matches in the string you would be none the wiser, unless you take action!

Explain:
There are a few very important things to remember when using heredoc.

Use <<< and some identifier that you choose to begin the heredoc. In this example we chose TEST as our identifier.
Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was TEST;
The closing sequence TEST; must occur on a line by itself and cannot be indented!

Another thing to note is that when you output this multi-line string to a web page, it will not span multiple lines because we did not have any <br /> tags contained inside our string! Here is the output made from the code above.

Wednesday, November 27, 2013

Basic PHP Syntax and Enable Short Tag in PHP.ini file


When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser. 

A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
You can use <? and ?> tag for PHP.
To use, you need enable "short_open_tags" in php.ini file.
The default file extension for PHP files is ".php".

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

<?phpecho "Hello PHP Tutorial Example Blog";
// ... more code
echo "Last statement";
// the script ends here with no PHP closing tag




A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:
Example:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to PHP Tutorial Example</h1>

<?php
echo "Hello PHP Tutorial Example Blog";
?>
</body>
</html>

Note: PHP statements are terminated by semicolon (;). The closing tag of a block of PHP code also automatically implies a semicolon (so you do not have to have a semicolon terminating the last line of a PHP block).

Comments in PHP

A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is editing the code!

Comments are useful for:

    To let others understand what you are doing - Comments let other programmers understand what you were doing in each step (if you work in a group)
    To remind yourself what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code

PHP supports three ways of commenting:
Code:
<!DOCTYPE html>
<html>
<body>

<?php
// This is a single line comment 
# This is also a single line comment 
/*
This is a multiple lines comment block
that spans over more than
one line
$message = "PHP Tutorial Example!";
*/
?>
</body>
</html>

PHP Case Sensitivity

In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-insensitive.

In the example below, all three echo statements below are legal (and equal):
Code:
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "PHP Tutorial Example!<br>";
echo "PHP for beginer!<br>";
EcHo "Lear PHP in one hour!<br>";
?>
</body>
</html>

However; in PHP, all variables are case-sensitive.

In the example below, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different variables):
Code:
<!DOCTYPE html>
<html>
<body>

<?php
$color="red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>

Monday, November 25, 2013

How to install Xampp step by step and test PHP code

To PHP installtion you can download Xampp or Wampp, I am introduce with xampp for windows.

Step 1: download xampp soft.
Link to download xampp: www.apachefriends.org/en/xampp-windows.html
Step 2: Install xampp server

When install success as image below.

Step 3: Start Xampp server:

Notice: install directory default is C:\xampp. In this directory includes:

We only notice "htdocs" directory beacause this directory contain your application.
Step 4: create a application "Welcome to PHP tutorial example"
In "htdocs" directory create a folder "hello" and in this folder create a file "index.php"
Open file "index.php" by notepad or editor other. Copy code bellow and parse into file "index.php"
<?php
echo "Welcome to PHP turorial example on blogger";
?>

Save and running application by open browser as firefox and type "http://localhost/hello/index.php": result as image bellow
Summary:
I was introduce install a simple web server and a application "hello". I hope this tutorial is understand for beginer.

If you have with problem install or writing code, please contact me via email: qgt2004@gmail.com

Thank all!