php

PHP – Loops – while – do…while – for – foreach

PHP – Loops – while – do…while – for – foreach

while

<?php 
$x=1; 
while($x<=5) // It executes the code as long as the specified condition is true.
  {
  echo "The number is: $x <br>";
  $x++;
  } 
?>

do…while

<?php 
$x=1; 
do
  {
  echo "The number is: $x <br>";
  $x++;
  }
while ($x<=5)  // It executes the code as long as the specified condition is true.
?>

NOTICE:
while -> 1. condition verification 2. code execution
do…while -> 1. code execution 2. condition verification

for

<?php 
for ($x=0; $x<=10; $x++)
  {
  echo "The number is: $x <br>";
  } 
?>

foreach

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

<?php 
$colors = array("red","green","blue","yellow"); 
foreach ($colors as $value)
  {
  echo "$value <br>";
  }
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Loops – while – do…while – for – foreach

PHP – Switch

PHP – Switch

<?php
$favcolor="red";
switch ($favcolor)
{
case "red":
  echo "Your favorite color is red!";
  break;
case "blue":
  echo "Your favorite color is blue!";
  break;
case "green":
  echo "Your favorite color is green!";
  break;
default:
  echo "Your favorite color is neither red, blue, or green!";
}
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Switch

PHP – If Else

PHP – If Else

<?php

// -----------
// if
// -----------

$t=date("H");
if ($t<"20")
  {
  echo "Have a good day!";
  }

// -----------
// if ... else
// -----------

$t=date("H");
if ($t<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }

// -----------
// if ... elseif ... else
// -----------

$t=date("H");
if ($t<"10")
  {
  echo "Have a good morning!";
  }
elseif ($t<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }

?>
By |PHP, Web Design|Commenti disabilitati su PHP – If Else

PHP – String Manipulation

PHP – String Manipulation

The most used functions for String Manipulation:

<?php

// ---------------------------------------------------------------

// String Operators

$a = "Hello";
$b = $a . " world!"; // Concatenation
echo $b; // outputs Hello world! 

$x="Hello";
$x .= " world!"; // Concatenation Assignement
echo $x; // outputs Hello world!

// ---------------------------------------------------------------

// strlen - To return the lenght of the string, blank spaces included 

echo strlen("Hello world!"); // the result is - 12 -

// ---------------------------------------------------------------

// strpos - To search for a specified character or text within a string.

// If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE.
// The first character position in the string is 0, and not 1.

echo strpos("Hello world!","world"); // the result is 6

// ---------------------------------------------------------------

// trim - Strip (Remove) whitespace (or other characters) from the beginning and end of a string

$text   = "Hello World";
echo $text;                 // Output: Hello World
echo '<br>';
echo trim($text, "Hello");  // Output: World
echo trim($text, "Hd");     // Output: ello Worl

// str_replace - Replace all occurrences of the search string with the replacement string

$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");
echo $newphrase = str_replace($healthy, $yummy, $phrase);
                            // Output: You should eat pizza, beer, and ice cream every day

// ---------------------------------------------------------------

// substr - Returns the portion of string specified by the start and length parameters.

echo substr("abcdef", -1);    // Output: "f"
echo substr("abcdef", -2);    // Output: "ef"
echo substr("abcdef", -3, 1); // Output: "d"

// ---------------------------------------------------------------

// stripslashes - Remove backslashes

$str = "Is your name O\'reilly?";
echo stripslashes($str);    // Output: Is your name O'reilly?

// ---------------------------------------------------------------

// strip_tags - Remove HTML and PHP tags

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo $text;                 // Output HTML formatted text
echo strip_tags($text);     // Output plain text

// ---------------------------------------------------------------

// strtolower - Make a string lowercase

$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Output: mary had a little lamb and she loved it so

// ---------------------------------------------------------------

?>  

All string functions at: http://www.php.net/manual/en/ref.strings.php

By |PHP, Web Design|Commenti disabilitati su PHP – String Manipulation

PHP – Echo – Print

PHP – Echo – Print

echo -> can output one or more strings, echo does not return any value.

print -> can only output one string, and returns always 1.


<?php

// echo of Strings
// ------------------
echo "<h2>PHP with HTML tags inside</h2>";
echo "This", " string", " was", " made", " with multiple parameters.";

// echo of variables
// ------------------
$txt1="I like PHP";
$txt2="blog.lucedigitale.com";
$cars=array("Volvo","BMW","Toyota");

echo $txt1;
echo "<br>";
echo "Study PHP at $txt2";
echo "My car is a {$cars[0]}";

// print of Strings
// ------------------
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";


// print of variables
// ------------------
$txt1="I like PHP";
$txt2="blog.lucedigitale.com";
$cars=array("Volvo","BMW","Toyota");

print $txt1;
print "<br>";
print "Study PHP at $txt2";
print "My car is a {$cars[0]}";

?>

By |PHP, Web Design|Commenti disabilitati su PHP – Echo – Print