Sunday, 18 February 2018

String in PHP

String is a sequence of characters.
It is denoted by single or double quote.
It is used to store and manipulate text.
E.g.1
<?php 
$name='LearningPoint92';  //denoted by single quote
echo $name; 
?> 
Output:
 LearningPoint92
E.g.2
<?php 
$name="LearningPoint92";  //denoted by double quotes
echo $name; 
?> 
Output:
 LearningPoint92

  String functions 
   There are some predefined functions in string like strlen(), strrev(), strpos() etc.

strlen() function

strlen() function returns the length of a string. In below example strlen() function is used to return length of "Hello India"
Example:
<?php
echo strlen("Hello India");
  ?>
Output:
11
str_word_count() function
str_word_count() function is used to count numbers of words in given string
Example:
<?php
echo str_word_count("Hello india");
?>
Output:
2
strrev() function
strrev() function is used to reverse a string
Example:
<?php
echo strrev("Hello india");
?>
Output:
aidnia olleH

strpos() function
strpos() function is used to search specific position of any words in given string
Example:
<?php
echo strpos("Hello india", "india");
?>
Output:
6

str_replace() function
str_replace function is used to replaces some characters with some other characters in a string. In below example we replace india with  world .
Example:
<?php
 echo str_replace("india", "world", "Hello india");
   ?>
Output:
  Hello world

strtolower() function
strtolower() function is used to convert uppercase letter into lowercase letter.
Example:
 <?php
  $str="Hey i am KAJOL"; 
$str=strtolower($str); 
echo $str; 
?>
Output:
 hey i am kajol
strtoupper() function
strtoupper() function is used to convert lowercase letter into uppercase letter
Example:
<?php
$str="hey i am kajol"; 
$str=strtoupper($str); 
echo $str; 
?>
Output:
HEY I AM KAJOL

ucwords() function

ucwords() function is used to convert first letter of every word into upper case.
Example:
<?php
$str="hey i am kajol"; 
$str=ucwords($str); 
echo $str; 
?>
Output:
Hey I Am Kajol

ucfirst() function

ucfirst() function converting first character into uppercase.
Example:
<?php
$str="hey i am kajol"; 
$str=ucfirst($str); 
echo $str; 
?>
Output:
Hey i am kajol

lcfirst() function

lcfirst() function converting first character into lowercase

Example:
<?php
$str="Hey i am kajol"; 
$str=lcfirst($str); 
echo $str; 
?>
Output:
hey I am kajol

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...