Friday, 13 April 2018

How to use global variable with function



PROGRAM:-
<?php
$name=”Castor Classes”;
echo $name;
function DisplayName()
{
global $name; # This is a global variable.
echo ‘ The name of online courses is Php’ .$name;
}
DisplayName(); #Call function
?>

OUTPUT:-
Castor Classes The name of online courses is PhpCastor Classes

How to use in-built function in PhP


Program:-

<html>
<head>
<meta charset=”UTF-8″>
<title>Castorclasses.com</title>
<style type = “text/css”>
</style>
</head>
<body>
<?
$username=”irawen.info@gmail.com”;
$entered= strtolower (“irawen.info@gmail.com”);
if($username==$entered)
{
echo “correct”;
}
else
{
echo “Please check your username”;
}
?>

How to use a basic function in PhP

Function:-A function is a block a code designed to perform a particular task.
Use:-To increase the readability of program and to perform a task repetitive manner.
Example:-
<?php
function hello_world()
{
echo”Hello world <br>”;         //This is a declare a function
echo”Hello world <br>”;
echo”Hello world <br>”;
}
hello_world();
?>

OUTPUT:-
Hello world
Hello world
Hello world

How to use function with return value

Program:-
<?php
function add($num1,$num2)
{
$result add  $num1 + $num2;
return result;
}
$add1=add(550+1000);
echo add (550,1000).'<br>’;
$add2=add(560+1000);
echo add (560,1000).'<br>’;
echo $add1*$add2;
?>

OUTPUT:-
1550
1560
2418000

Switch Statement use in PhP


  • PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.
Syntax:-
switch(expression){
case value1:
 //code to be executed
 break;
case value2:
 //code to be executed
 break;
……
default:
 code to be executed if all cases are not matched;
}

Example:-
 
<?php
$num=20;
switch($num){
case 10:
echo(“number is equals to 10”);
break;
case 20:
echo(“number is equal to 20”);
break;
case 30:
echo(“number is equal to 30”);
break;
default:
echo(“number is not equal to 10, 20 or 30”);
}
?>

Output:
number is equal to 20

Loop use in PhP

For Loop

  • PHP for loop can be used to traverse set of code for the specified number of times.
  • It should be used if number of iteration is known otherwise use while loop 
  • Syntax:-
            for(initialization; condition; increment/decrement){
            //code to be executed
               }

Example:-
<?php
for($n=1;$n<=10;$n++){
echo “$n<br/>”;
}
?>


OUTPUT:-
1
2
3
4
5
6
7
8
9
10

While Loop use in PhP

  • while loop can be used to traverse set of code like for loop.
  • it should be used if number of iteration is not known.
syntax:-
while(condition){
//code to be executed
}
another  syntax:-
while(condition):
//code to be executed
endwhile;

Example:-
<?php

$n=1;
while($n<=10){
echo “$n”;

$n++;

}

?>


OUTPUT:-

1

2

3

4

5

6

7

8

9

10

How to array use in a PhP?

  • Array is a collection Similar data,Basically array is use for group of value.
Example:-
<?php
$names=array(‘mark’,’john’,’bob’);
echo $names[1];
?>

OUTPUT:-
john

  • The another methods is-
Example:-
<?php
$names=array(‘Mark’,’John’,’Bob’);
$names[3]=’July’;                                  //This is use for add another value.
$names[4]=’Castor Classes’;                 //This is use for add another value.
print_r($names);                                   //Print all value on output screen.
?>

OUTPUT:-
Array ( [0] => Mark [1] => John [2] => Bob [3] => July [4] => Castor Classes )

Multidimensional Array in PhP

  • PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.
Syntax:-
$emp = array
  (
  array(1,“sonoo”,400000),
  array(2,“john”,500000),
  array(3,“rahul”,300000)
  );
Example:-
<?php
/*
students
Name Age Weight
Mark 15 46
John 13 65
Tom 14 56
*/
$students=array(array(‘Name’=>’Mark’,’Age’=>15,’Weight’=>46),
array(‘Name’=>’John’,’Age’=>13,’Weight’=>65),
array(‘Name’=>’Tom’,’Age’=>14,’Weight’=>56));
echo $students[0][‘Name’].’ ‘;
echo $students[1][‘Age’].’ ‘;
echo $students[2][‘Weight’].'<br>’;
echo $students[0][‘Name’].’ ‘;
echo $students[1][‘Age’].’ ‘;
echo $students[2][‘Weight’].'<br>’;
echo $students[0][‘Name’].’ ‘;
echo $students[1][‘Age’].’ ‘;
echo $students[2][‘Weight’].'<br>’;
?>

OUTPUT:-
Mark 15 46
Mark 14 65
Mark 13 56

How can use Comparison operator and Logical Operator?


  • The Comparison operator is used for compare between two number.
  • There are study a  some operators:-

    Example:-
    <?php
    $number1=’10’;
    $number2=10;
    if($number1==$number2)
    {
    echo “Equal”;
    }
    else
    {
    echo “Not Equal”;
    }
    ?>
    OUTPUT:-
    Equal

    Logical Operators
  • The logical operator is used for logic apply between to equation.
  • Here, we are see a some operators:-


Example:-
<?php
$number1=20;
$number2=10;
if(($number1>$number2)and($number1==$number2))
{
echo “Equal”;
}
else
{
echo “Not Equal”;
}
?>
OUTPUT:-
Not Equal

String is Empty Example



  1. /*
            Java String isEmpty Example.
            This Java String isEmpty example shows how to check whether the given
            string is empty or not using isEmpty method of Java String class.
     */
    public class JavaStringIsEmptyExample {
            public static void main(String args[]){

                    String str1 = “”;
                    String str2 = null;
                    String str3 = “Hello World”;

                    /*
                     * To check whether the String is empty or not, use
                     * boolean isEmpty() method of Java String class.
                     *
                     * This method returns true if and only if string.length() is 0.
                     */

                    System.out.println(“Is String 1 empty? :” + str1.isEmpty());

                    //this will throw NullPointerException since str2 is null
                    //System.out.println(“Is String 2 empty? :” + str2.isEmpty());
                   
                    System.out.println(“Is String 3 empty? :” + str3.isEmpty());

                    /*
                     * Please note that isEmpty method was added in JDK 1.6 and it is not available
                     * in previous versions.
                     *
                     * However, you can use  
                     * (string.length() == 0) instead of (string.isEmpty())
                     * in previous JDK versions.
                     */
            }
    }

    /*
    Output of Java String isEmpty would be
    Is String 1 empty? :true
    Is String 3 empty? :false
    */

String to Date Example




  •         Java String to Date Example.
            This Java String to Date example shows how to convert Java String to Date object.
     */
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class JavaStringToDate {
            public static void main(String args[]){

                    //Java String having date
                    String strDate = “21/08/2011”;

                    /*
                     * To convert Java String to Date, use
                     * parse(String) method of SimpleDateFormat class.
                     *
                     * parse method returns the Java Date object.
                     */

                    try{

                            /*
                             * Create new object of SimpleDateFormat class using
                             * SimpleDateFormat(String pattern) constructor.
                             */

                            SimpleDateFormat sdf = new SimpleDateFormat(“dd/MM/yyyy”);

                            //convert Java String to Date using parse method of SimpleDateFormat
                            Date date = sdf.parse(strDate);

                            //Please note that parse method throws ParseException if the String date could not be parsed.

                            System.out.println(“Date is: “ + date);

                    }catch(ParseException e){
                            System.out.println(“Java String could not be converted to Date: “ + e);
                    }
            }
    }

    /*
    Output of Java String to Date example would be
    Fri Jan 21 00:00:00 IST 2011
    */

String to Lower Case example



  1. /*
            Java String to Lower Case example.
            This Java String to Lower Case example shows how to change the string to lower case
            using toLowerCase method of String class.
    */
    public class StringToLowerCaseExample {

            public static void main(String[] args) {

                    String str = “STRING TOLOWERCASE EXAMPLE”;

                    /*
                     * To change the case of string to lower case use,
                     * public String toLowerCase() method of String class.
                     *
                     */

                     String strLower = str.toLowerCase();

                     System.out.println(“Original String: “ + str);
                     System.out.println(“String changed to lower case: “ + strLower);
            }
    }

    /*
    Output would be
    Original String: STRING TOLOWERCASE EXAMPLE
    String changed to lower case: string tolowercase example
    */

String to UpperCase example



  1. /*
            Java String to Upper Case example.
            This Java String to Upper Case example shows how to change the string to upper case
            using toUpperCase method of String class.
    */

    public class StringToUpperCaseExample  
    {

            public static void main(String[] args)  
    {

                    String str = “string touppercase example”;

                    /*
                     * To change the case of string to upper case use,
                     * public String toUpperCase() method of String class.
                     *
                     */

                     String strUpper = str.toUpperCase();

                     System.out.println(“Original String: “ + str);
                     System.out.println(“String changed to upper case: “ + strUpper);
            }
    }

    /*
    Output would be
    Original String: string touppercase example
    String changed to upper case: STRING TOUPPERCASE EXAMPLE*/

String Trim Example



  1. /*
            Java String Trim Example.
            This Java String trim example shows how to remove leading and trailing space
            from string using trim method of Java String class.
    */

    public class RemoveLeadingTrailingSpace  
    {

            public static void main(String[] args)  
    {

                    String str = ”   String Trim Example   “;

                    /*
                     * To remove leading and trailing space from string use,
                     * public String trim() method of Java String class.
                     */

                     String strTrimmed = str.trim();

                     System.out.println(“Original String is: “ + str);
                     System.out.println(“Removed Leading and trailing space”);
                     System.out.println(“New String is: “ + strTrimmed);
            }
    }

    /*
    Output would be
    Original String is:    String Trim Example  
    Removed Leading and trailing space
    New String is: String Trim Example
    */

String Split Example



*
  • Java String split example.
    This Java String split example describes how Java String is split into multiple
    Java String objects.
    */

    public class JavaStringSplitExample{

      public static void main(String args[]){
      /*
      Java String class defines following methods to split Java String object.
      String[] split( String regularExpression )
      Splits the string according to given regular expression.
      String[] split( String reularExpression, int limit )
      Splits the string according to given regular expression. The number of resultant
      substrings by splitting the string is controlled by limit argument.
      */
      /* String to split. */
      String str = “one-two-three”;
      String[] temp;

      /* delimiter */
      String delimiter = “-“;
      /* given string will be split by the argument delimiter provided. */
      temp = str.split(delimiter);
      /* print substrings */
      for(int i =0; i < temp.length ; i++)
        System.out.println(temp[i]);

      /*
      IMPORTANT : Some special characters need to be escaped while providing them as
      delimiters like “.” and “|”.
      */

      System.out.println(“”);
      str = “one.two.three”;
      delimiter = \\.”;
      temp = str.split(delimiter);
      for(int i =0; i < temp.length ; i++)
        System.out.println(temp[i]);

      /*
      Using second argument in the String.split() method, we can control the maximum
      number of substrings generated by splitting a string.
      */
      System.out.println(“”);
      temp = str.split(delimiter,2);
      for(int i =0; i < temp.length ; i++)
        System.out.println(temp[i]);

      }

    }

    /*
    OUTPUT of the above given Java String split Example would be :
    one
    two
    three
    one
    two
    three
    one
    two.three
    */

String Replace Example



  1. /*
    Java String replace example.
    This Java String Replace example describes how replace method of java String class
    can be used to replace character or substring can be replaced by new one.
    */
    public class JavaStringReplaceExample{

      public static void main(String args[]){

        /*
        Java String class defines three methods to replace character or substring from
        the given Java String object.
        1) String replace(int oldChar, int newChar)
        This method replaces a specified character with new character and returns a
        new string object.
        2) String replaceFirst(String regularExpression, String newString)
        Replaces the first substring of this string that matches the given regular
        expression with the given new string.
        3) String replaceAll(String regex, String replacement)
        Replaces the each substring of this string that matches the
        given regular expression with the given new string.
        */

        String str=“Replace Region”;

        /*
        Replaces all occourances of given character with new one and returns new
        String object.
        */
        System.out.println( str.replace( ‘R’,‘A’ ) );

        /*
        Replaces only first occourances of given String with new one and
        returns new String object.
        */
        System.out.println( str.replaceFirst(“Re”, “Ra”) );

        /*
        Replaces all occourances of given String with new one and returns
        new String object.
        */
        System.out.println( str.replaceAll(“Re”, “Ra”) );

      }

    }

    /*
    OUTPUT of the above given Java String Replace Example would be :
    Aeplace Aegion
    Raplace Region
    Raplace Ragion
    */

String Length Example



  1. /*
      Java String Length Example
      This example shows how to get a length of a given String object.
    */

    public class StringLengthExample 
    {

      public static void main(String[] args)  
    {
        //declare the String object
        String str = “Hello World”;

        //length() method of String returns the length of a String.
        int length = str.length();
        System.out.println(“Length of a String is : “ + length);
      }
    }

    /*
    Output of a program would be:
    Length of a String is : 11
    */

String Contains example





  •        This Java String contains examples shows how to use contains method of Java String class.
     */
    public class JavaStringContains {
            public static void main(String args[]){
                    String str1 = “Hello World”;
                    String str2 = “Hello”;

                    /*
                     * To check whether the string contains specified character sequence use,
                     * boolean contains(CharSequence sq)
                     * method of Java String class.
                     *
                     * This method returns true if the string contains given character sequence.
                     * Please note that contains method is added in Java 1.5  
                     */
                   
                    boolean blnFound = str1.contains(str2);
                    System.out.println(“String contains another string? : “ + blnFound);

                    /*
                     * Please also note that the comparison is case sensitive.
                     */
                   
            }
    }

    /*
    Output of Java String contains example would be
    String contains another string? : true
    */

Popular Posts

Categories

100 Python Programs for Beginner (56) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (935) Python Coding Challenge (368) Python Quiz (27) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses