Showing posts with label PHP. Show all posts

Array Exercises and Examples of Arrays using Php

  No comments
16:43

Array is a set of variables that have the same data type and are declared with the same name. Array is an important concept in programming, because the array allows to store data and object reference in large quantities and indexed.
The following is an example example of the use of Array, as follows:

Example:

<?php
$year = array(1924, 2010, 2012, 2024, 2050);
echo $year[3];
?>

Example1:
<?php
$student = array("Eka Haryanto", "Lakilaki", "
11/11/1993 "," B ", 3.41," debageur ");
echo $student[0]. "<br>";
echo $student[1]. "<br>";
echo $student[2]. "<br>";
echo $student[3]. "<br>";
echo $student[4]. "<br>";
echo $student[5]. "<br>";
?>

Example2:
<?php
$student = array("Eka Haryanto", "Lakilaki", "
11/11/1993 "," B ", 3.41," debageur ");
for($x = 0; $x <= 6; $x ++)
{
echo $student[$x]. "<br>";
}
?>

Example3: Array Next
<?php
$student = array("Eka Haryanto", "Lakilaki", "
11/11/1993 "," B ", 3.41," debageur ");
For($x = 0; $x <6; $x ++)
{
echo "Index of student array to:" .key($student). "Contains
".current($student)." <br> ";
sext($student);
}
?>

Example3: Array prev
<?php
$student = array("Eka Haryanto", "Lakilaki", "
11/11/1993 "," B ", 3.41," debageur ");
End($student);
For($x = 0; $x <6; $x ++)
{
echo "The student array index to:" .key($student). "Contains
".current($student)." <br> ";
Prev($student);
}
?>

Example4: Number of Arrays
<?php
$Student = array("Eka Haryanto", "Lakilaki", "
11/11/1993 "," B ", 3.41," debageur ");
$Sumarray = count($student);
echo $sumarray;
?>

Example5: Accessing Arrays In separate variables
<?php
$Student = array("Eka Haryanto", "Lakilaki", "
11/11/1993 "," B ", 3.41," debageur ");
List($name, $sex type, $responder, $points, $IP, $specialization) =
$student;
echo $name. "<br>";
echo $jeniskelamin. "<br>";
echo $respon. "<br>";
echo $points. "<br>";
echo $IP. "<br>";
echo $specialization. "<br>";
?>

Example6: Array With Foreach
<?php
$student = array("Eka Haryanto", "Lakilaki", "
11/11/1993 "," B ", 3.41," debageur ");
foreach($student as $datamahasiswa)
{
echo $datamahasiswa. "<br>";
}
?>

Example7: Sort Arrays
<?php
$Data = array(1,3,2,4,7,8,6,5,9,10);
sort($data);
for($x = 0; $x <= 10; $x ++)
{
echo current($data). "<br>";
next($data);
}
?>

Such are examples of the use of arrays, Keep to practice it, Happy coding, and fighting :)

Read More

Examples of Random Functions In PHP

  No comments
13:54

The random function is used to randomize a character, to create a random with Integer / Figures type can use the default function of PHP that is with RAND (min, max) as an example:

    <?php
    echo rand (1.10);
    ?>

Then its output is 1-10

But how to make random with letters / alphabets?
The author has not found any special function for random alphabet / letters from PHP, but There are many ways to random functions with these letters / alphabets, including:
Example 1:

    function random ($long) {
       $character = 'ABCDEFGHIJKLMNOPQRSTUabcdefghijklmnopqrstuvwxyz1234567890';
       $String = '';
       For ($i = 0; $i <$long; $i ++) {
           $pos = rand (0, strlen ($character) -1);
           $string. = $Character {$mail};
       }
       return $string;
    }
    echo random (11); // The output of numbers and random letters is 11 characters


 Example 2:

    <?php
    $a1 = str_repeat ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5);
    $a2 = str_shuffle ($a1);
    echo substr ($a2,0,10); // The output is random numbers and letters of 10 characters
    ?>

 Example 3:

    for ($i = 1; $i <= 10; $i ++) echo chr (rand (97,122));

Read More

Common Functions Frequently Used In PHP

  No comments
15:18



Hundreds of functions provided by PHP as well as thousands of others are available through various additional extensions. These functions are documented in the PHP documentation. However, in varying degrees of development, it now has various naming conventions. The function syntax is as below:

STRING FUNCTIONS
String functions are used to manipulate strings for various needs. Here will be discussed some string functions that are often used in creating a web application program.

AddSlashes
Used to add a backslash(\) character to a string. It is important to use the query string for the database, for example in MySQL. Some of the characters to which the backsearches are added are the quotes of one('), double quoted characters("), backslash(\) and NULL characters.

Syntax:
Addslashes(string)

StripSlashes
Used to remove the backslash(\) character on a string.

Syntax:
String stripslashes(string)

Crypt
Used to encrypt the DES method of a string. This function is often used to shuffle password strings before they are stored in the database. In the use of this crypt function can be added string parameter 'salt'. This 'salt' parameter is added to determine the random basis. The 'Salt' string consists of 2 characters. If the 'salt' string is not added to the crypt function then PHP will determine itself 'salt' the string randomly.

Syntax:
Crypt(string [, salt])

Echo
Used to print the contents of a string or argument.

Syntax:
Echo(string argument1, string argument2, ....)

Explode
Used to break up a string based on a specific separator and insert the result into a variable array.

Syntax:
Explode(string separator, string [, int limit])

Example:
$namahari = "Monday monday on Wednesday thursday saturday";
$days = explode("", $namahari);

Implode
The usefulness of this function is the reverse of the explode function. The implode function is used to generate a string of each element of an array. The resulting string is separated by a predefined string.

Syntax:
Implode(string separator, array)

Strip_Tags
Used to remove HTML tag codes on a string.

Syntax:
Striptags(string [, string tags not omitted])

StrLen
Used to count the number of characters of a string.

Syntax:
Strlen(string)

StrPos
Used to find the first position of a sub string on a string. This function is usually used to search for a sub string within a string.

Syntax:
Strlen(string, sub string)

Str_Repeat
Used to repeat the contents of a string.

Syntax:
Str_repeat(string, int loop count)

StrToLower
Used to convert a string into lowercase.

Syntax:
Strtolower(string)

StrToUpper
Used to convert a string into uppercase(uppercase)

Syntax:
Strtoupper(string)

SubStr
Used to retrieve a sub string with a certain length of a string in a certain position as well.

Syntax:
Substr(string, int position, int position)

Example:
Substr("abcdefg", 0.3); // produces string "abc" substr("abcdefg", 3,2); // generate string "de"

SubStr_Count

Used to count the number of sub strings in a string

Syntax:
Substr_count(string, substring string)

Example:
Substr_count("This is a test", "is"); // returns a value of 2

UCFirst

Used to replace the first character on a string to uppercase.

Syntax:
Ucfirst(string)

UCWords
Used to replace the first character of each word in a string to uppercase.

Syntax:
Ucwords(string)


FUNCTION DATE
Used to retrieve date and time. The result of this function is a string containing the date / time according to the desired format. The known formats in this date function are as follows:



ƒ    a - "am" or "pm" 
ƒ    A - "AM" or "PM" 
ƒ    B - Swatch Internet time 
ƒ    d - day of the month, 2 digits with leading zeros; i.e. "01" to "31" 
ƒ    D - day of the week, textual, 3 letters; i.e. "Fri" 
ƒ    F - month, textual, long; i.e. "January" 
ƒ    g - hour, 12-hour format without leading zeros; i.e. "1" to "12" 
ƒ    G - hour, 24-hour format without leading zeros; i.e. "0" to "23" 
ƒ    h - hour, 12-hour format; i.e. "01" to "12" 
ƒ    H - hour, 24-hour format; i.e. "00" to "23" 
ƒ    i - minutes; i.e. "00" to "59" 
ƒ    I (capital i) - "1" if Daylight Savings Time, "0" otherwise. 
ƒ    j - day of the month without leading zeros; i.e. "1" to "31" 
ƒ    l (lowercase 'L') - day of the week, textual, long; i.e. "Friday" 
ƒ    L - boolean for whether it is a leap year; i.e. "0" or "1" 
ƒ    m - month; i.e. "01" to "12" 
ƒ    M - month, textual, 3 letters; i.e. "Jan" 
ƒ    n - month without leading zeros; i.e. "1" to "12" 
ƒ    s - seconds; i.e. "00" to "59" 
ƒ    S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd" 
ƒ    t - number of days in the given month; i.e. "28" to "31" 
ƒ    T - Timezone setting of this machine; i.e. "MDT" 
ƒ    U - seconds since the epoch 
ƒ    w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday) 
ƒ    Y - year, 4 digits; i.e. "1999" 
ƒ    y - year, 2 digits; i.e. "99" 
ƒ    z - day of the year; i.e. "0" to "365" 
ƒ    Z - timezone offset in seconds (i.e. "-43200" to "43200") 




Syntax:
date(string format)

Example:
Date("Y-m-d"); // generates "2001-07-28" date("l, j F Y"); // generate "Saturday, 28 July 2001" date("H: i: s"); // generate "20:15:07"


FUNCTION MAIL
Used to send e-mail to a specific e-mail address.

Syntax:
Mail(destination string, string subject, string contents [, string header]);

Example:
$sender = "From: me@email.com";
$to = "qody@gmail.com";
$subject = "Notice";
$content = "This is an experiment of sending e-mail using PHP";
mail($to, $subject, $content, $sender);

Read More

Learn Basic PHP Programming Language With Logic

  No comments
15:07



PHP is a scripting language that integrates with HTML and runs on a server side. This means that all the syntax we provide will be fully executed on the server while sent to the browser only the result only.




Here are some examples of basic PHP scripts that can be learned.

Example1.php file:

<html>
<head>
<title> Simple Example </ title>
</head>
<body> <?php
echo("Hallo How are you ? I'm Learning PHP script");
?>
</body> </ html>


 

VARIABLE
In PHP each variable name begins with a dollar sign($). For example the variable name a in PHP is written with $a. The type of a variable is determined when the program runs and depends on the context used.


Example2.php file:

<?php
$a = "5"; $b = "2"; $result = $a + $b;
echo($results);
 ?>
The result is: 7
  
Example3.php file:

<?php
$a = "5"; $b = "2"; $result = $a. $b;
 echo($results);
?>
The result is: 52

 
CONTROL STRUCTURE

IF
IF Construction is used to perform conditional execution of a statement. The way of writing is as follows:

If(condition)
     {
     Statement
     }

or:
     If(condition)
     {
     Statement
     }
     Else
     {
     Another statement
     }

or:
     If(first condition)
     {
     First statement
     }
     Elseif(second condition)
     {
     Second statement
     }
     Else
     {
     Another statement
     }

Example5.php file:

<?php
$a = 4; $B = 9; If($a> $b)
     {
     echo("a is bigger than b");
     }
Elseif($a <$b)
     {
     echo("a smaller b");
     }
Else
     {
     echo("a is equal to b");
     }
?>

The result is: a smaller b




WHILE
The basic form of the While statement is as follows:

while(terms)
     {
     statement
     }

The meaning of statemant While is giving the command to run the statement under it repeatedly, as long as the conditions are met.

Example file6.php:

<?php $a = 1;
while($a <10)
     {
     echo($a);
     $a ++;
     }
?>


The result is: 123456789
 
FOR
How to write FOR statements are as follows:

For(expression1; expression2; expression3) statement

Expression1 indicates the initial value for a variable
Expression2 indicates the condition that must be fulfilled to run statemant expression3 shows the value added for a variable

Example file7.php:

<?php
     For($a = 0; $a <10; $a ++)
     {
     echo("Value A ="); echo("$a");
     echo("<br>");
}
?>

The result is:
Value A = 1
Value A = 2
Value A = 3
Value A = 4
Value A = 5
Value A = 6
Value A = 7
Value A = 8
Value A = 9

SWITCH
The SWITCH statement is used to compare a variable with multiple values ​​and execute a particular statement if the value of a variable equals the value being compared.
Switch structure is as follows:

Switch(variable) case value: statement case value: statemant case value: statement
     .
     .
     .

Example8.php file:

<?php $a = 2; Switch($a)
     {
     Case 1:
     echo("The value of variable a is one");
     break; Case 2:
     echo("The value of variable a is two");
     break; Case 3:
     echo("The value of variable a is three");
     break;
     }
?>

The result is: The value of variable a is two



REQUIRE
Statement Require is used to read the value of variables and functions of another file. How to write a statement Require is:

     Require(filename);

This Require statement can not be entered in a looping structure such as while or for. Because it only allows calling the same file only once.

Example9.php file:

<?php
$a = "I'm learning PHP";

function tulistebal($text)
{
echo("<b> $text </ b>");
}
?>

Example file10.php:

<?php
require("contoh9.php");
tulistebal("This is bold");
echo("<br>");
echo($a); ?>




INCLUDE
The include statement will include the contents of a particular file. include can be placed inside a loop such as in the for or while statement.

Example11.php file:

<?php
echo("-------------------------------------- <br>"); echo("PHP is a scripting language <br>"); echo("-------------------------------------- <br>"); echo("<br>");
?>

Example12.php file:

<?php
for($b = 1; $b <5; $b ++)
     {
     include("contoh11.php");
     }
?>

So basic PHP along with its easy logic easy to understand, apologize if there is mistake.
 

Read More