Some String Functions in Php
1. String
Length in Php:
<?php
echo strlen("Hello world!");
?>
echo strlen("Hello world!");
?>
2. String
Position
<?php
echo strpos("Hello world!","world");
?>
echo strpos("Hello world!","world");
?>
3. String
Constant
<?php
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING; // not Case-sensitive.
echo greeting;
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING; // not Case-sensitive.
echo greeting;
?>
Note: Every time we
echo GREETING it will print Welcome to W3Schools.com!
4. Some
Tricky arithmetic operations are
<?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
?>
$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
?>
5. String
Operations
<?php
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world!
$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world!
?>
$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world!
$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world!
?>
6. PHP
foreach loop
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
{
echo "$value <br>";
}
?>
$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
{
echo "$value <br>";
}
?>
7. User
Defined Function in PHP
<?php
function writeMsg()
{
echo "Hello world!";
}
writeMsg(); // call the function
?>
function writeMsg()
{
echo "Hello world!";
}
writeMsg(); // call the function
?>
8.
Function argument
<?php
function familyName($fname)
{
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
function familyName($fname)
{
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
No comments:
Post a Comment