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

PHP – Operators

PHP – Operators

PHP Arithmetic Operators

<?php 
$x=10; 
$y=6;
echo ($x + $y); // outputs 16
echo ($x - $y); // outputs 4
echo ($x * $y); // outputs 60
echo ($x / $y); // outputs 1.6666666666667 
echo ($x % $y); // outputs 4 
?>

Assignement Operators

<?php 
$x=10; 
echo $x; // outputs 10

$y=20; 
$y += 100;
echo $y; // outputs 120

$z=50;
$z -= 25;
echo $z; // outputs 25

$i=5;
$i *= 6;
echo $i; // outputs 30

$j=10;
$j /= 5;
echo $j; // outputs 2

$k=15;
$k %= 4;
echo $k; // outputs 3
?>

String Operators

<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world! 

$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world! 
?>

Increment/Decrement Operators

<?php
$x=10; 
echo ++$x; // outputs 11 first  -> the increment starts
echo ++$x; // outputs 12        -> the incrementt continues...

$y=10; 
echo $y++; // outputs 10 first  -> the value in the same
echo $y++; // outputs 11 second -> the increment starts

$z=5;
echo --$z; // outputs 4 first   -> the decrement starts
echo --$z; // outputs 3 second  -> the decrement continues...

$i=5;
echo $i--; // outputs 5 first  -> the value in the same
echo $i--; // outputs 4 second -> the decrement starts
?>

Comparison Operators

== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y True if $x is greater than $y
< Less than $x < $y True if $x is less than $y >= Greater than or equal to $x >= $y True if $x is greater than or equal to $y
<= Less than or equal to $x <= $y True if $x is less than or equal to $y [php] <?php $x=100; // It is an integer number $y="100"; // It is a string var_dump($x == $y); // Output: bool(true) echo "<br>"; var_dump($x === $y); // Output: bool(false) echo "<br>"; var_dump($x != $y); // Output: bool(false) echo "<br>"; var_dump($x !== $y); // Output: bool(true) echo "<br>"; $a=50; $b=90; var_dump($a > $b); // Output: bool(false) echo "<br>"; var_dump($a < $b); // Output: bool(true) ?> [/php]

Logical Operators

and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true

Array Operators

+ Union $x + $y Union of $x and $y (but duplicate keys are not overwritten)
== Equality $x == $y True if $x and $y have the same key/value pairs
=== Identity $x === $y True if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y True if $x is not equal to $y
<> Inequality $x <> $y True if $x is not equal to $y
!== Non-identity $x !== $y True if $x is not identical to $y

<?php
$x = array("a" => "red", "b" => "green"); 
$y = array("c" => "blue", "d" => "yellow"); 
$z = $x + $y;   
     
var_dump($z);        // Output: array(4) { ["a"]=> string(3) "red" ["b"]=> string(5) "green" ["c"]=> string(4) "blue" ["d"]=> string(6) "yellow" }

var_dump($x == $y);  // Output: bool(false) 
var_dump($x === $y); // Output: bool(false) 
var_dump($x != $y);  // Output: bool(true) 
var_dump($x <> $y);  // Output: bool(true) 
var_dump($x !== $y); // Output: bool(true) 
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Operators

PHP – Constants

PHP – Constants

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

Unlike variables, constants are automatically global across the entire script.

To set a constant, use the define() function.

Statement:

define(“GREETING”, “Welcome to W3Schools.com!”, true);

define(“constant name”, “constat value”, case-insensitive true/false);


<?php

// define a case-insensitive constant
define("GREETING", "Welcome to W3Schools.com!", true);
echo GREETING;
echo "<br>";
// will also output the value of the constant
echo greeting; // case-insensitive

?>  

By |PHP|Commenti disabilitati su PHP – Constants