Basics of PHP
1) Variables:
Variables are used for storing values, like text strings, numbers or arrays.When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP: $var_name = value;
2) String Variables:
String variables are used for values that contain characters.
After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
Below, the PHP script assigns the text "Hello World" to a string variable called $txt:
<?php
$txt="Hello World";
echo $txt;
?>
After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
Below, the PHP script assigns the text "Hello World" to a string variable called $txt:
<?php
$txt="Hello World";
echo $txt;
?>
3)Operators: Operators are used to operate on values.
1. Arithmetic Operator:
Arithmetic operators are used to perform simple mathematical operators.
Example:
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
Example:
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
2. Assignment Operator:
Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character.
Example:
• $my_var = 4;
• $another_var = $my_var;
Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.
Example:
• $my_var = 4;
• $another_var = $my_var;
Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.
3. Comparison Operator:
Comparisons are used to check the relationship between variables and/or values.Comparison operators are used inside conditional statements and evaluate to either true or false.Here are the most important comparison operators of PHP.
Assume: $x = 4 and $y = 5;
Assume: $x = 4 and $y = 5;
4. Logical Operator:
The logical operators compare Boolean expressions and return a Boolean result.
Examples:
1) concat.php (concat two String)
<?php
//Double lines indicates that it is a comment
/* Indicates multiline Comment*/
$str1="This is Programmingkatta Blog";
$str2="written to gain Knowledge";
$str3=$str1." ".$str2;
echo"$str3";
//The above code will give First Line of Output
//-----------------------2nd line output------------------------
echo "<br />";//<br />-Use to break a line
$aray=array();//array()-fuction to declare it as array.
$aray[0]="Makarand";
echo $aray[0];
//--------------------------------------------------------------
echo "<br />";
$name="Aishwarya";
echo "1. My name is ".$name;//3rd line output
echo "<br />";
echo "2. My name is ".$aray[0];//4th line output
echo "<br />";
echo "3. My name is $name";//5th line output
//--------------------------------------------------------------
echo "<br />";
echo "<input type=\"text\" id=\"name\" value=\"$name\" />";
echo "<br />";
echo "<input type=\"text\" id=\"name2\" value=\"".$aray[0]."\">";
echo "<br />";
echo "This is example of split string";
?>
//===============================================================
2) maths.php (Simple mathematical operations using PHP)
<?php
$x=30;
$y=20;
$a=$x+$y;
$b=$x-$y;
$c=$x*$y;
$d=$x/$y;
$e=$x%$y;
echo"x=$x<br />";
echo"y=$y<br />";
echo"x+y=$a<br />";//add
echo"x-y=$b<br />";//sub
echo"x*y=$c<br />";//mul
echo"x/y=$d<br />";//div
echo"x%y=$e<br />";//modulus
?>
$x=30;
$y=20;
$a=$x+$y;
$b=$x-$y;
$c=$x*$y;
$d=$x/$y;
$e=$x%$y;
echo"x=$x<br />";
echo"y=$y<br />";
echo"x+y=$a<br />";//add
echo"x-y=$b<br />";//sub
echo"x*y=$c<br />";//mul
echo"x/y=$d<br />";//div
echo"x%y=$e<br />";//modulus
?>
//===============================================================
3)Display array data using table
<?php
echo"<h1>Records:</h1>";
$firstname=array();
$firstname[0]="Makarand";
$firstname[1]="Bhumika";
$firstname[2]="Shweta";
$firstname[3]="Athul";
$firstname[4]="Ketan";
$lastname=array();
$lastname[0]="Dalal";
$lastname[1]="Dhotre";
$lastname[2]="Gupta";
$lastname[3]="Pavithran";
$lastname[4]="Kasar";
$mobilenumber=array();
$mobilenumber[0]=6785234265;
$mobilenumber[1]=5893214785;
$mobilenumber[2]=1423879654;
$mobilenumber[3]=7851236549;
$mobilenumber[4]=4582316578;
$email=array();
$email[0]="vishakha@gmail.com";
$email[1]="charvi@gmail.com";
$email[2]="rucha@gmail.com";
$email[3]="radhika@gmail.com";
$email[4]="riddhi@gmail.com";
$city=array();
$city[0]="Mumbai";
$city[1]="Delhi";
$city[2]="Kolkata";
$city[3]="Pune";
$city[4]="Banglore";
//To create table and call values in table Below code is used
echo "<table border=\"8\">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Mobile Number</th>
<th>Email</th>
<th>City</th>
</tr>
<tr><td>$firstname[0]</td>
<td>$lastname[0]</td>
<td>$mobilenumber[0]</td>
<td>$email[0]</td>
<td>$city[0]</td></tr>
<tr><td>$firstname[1]</td>
<td>$lastname[1]</td>
<td>$mobilenumber[1]</td>
<td>$email[1]</td>
<td>$city[1]</td></tr>
<tr><td>$firstname[2]</td>
<td>$lastname[2]</td>
<td>$mobilenumber[2]</td>
<td>$email[2]</td>
<td>$city[2]</td></tr>
<tr><td>$firstname[3]</td>
<td>$lastname[3]</td>
<td>$mobilenumber[3]</td>
<td>$email[3]</td>
<td>$city[3]</td></tr>
<tr><td>$firstname[4]</td>
<td>$lastname[4]</td>
<td>$mobilenumber[4]</td>
<td>$email[4]</td>
<td>$city[4]</td></tr>
</table>";
?>
echo"<h1>Records:</h1>";
$firstname=array();
$firstname[0]="Makarand";
$firstname[1]="Bhumika";
$firstname[2]="Shweta";
$firstname[3]="Athul";
$firstname[4]="Ketan";
$lastname=array();
$lastname[0]="Dalal";
$lastname[1]="Dhotre";
$lastname[2]="Gupta";
$lastname[3]="Pavithran";
$lastname[4]="Kasar";
$mobilenumber=array();
$mobilenumber[0]=6785234265;
$mobilenumber[1]=5893214785;
$mobilenumber[2]=1423879654;
$mobilenumber[3]=7851236549;
$mobilenumber[4]=4582316578;
$email=array();
$email[0]="vishakha@gmail.com";
$email[1]="charvi@gmail.com";
$email[2]="rucha@gmail.com";
$email[3]="radhika@gmail.com";
$email[4]="riddhi@gmail.com";
$city=array();
$city[0]="Mumbai";
$city[1]="Delhi";
$city[2]="Kolkata";
$city[3]="Pune";
$city[4]="Banglore";
//To create table and call values in table Below code is used
echo "<table border=\"8\">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Mobile Number</th>
<th>Email</th>
<th>City</th>
</tr>
<tr><td>$firstname[0]</td>
<td>$lastname[0]</td>
<td>$mobilenumber[0]</td>
<td>$email[0]</td>
<td>$city[0]</td></tr>
<tr><td>$firstname[1]</td>
<td>$lastname[1]</td>
<td>$mobilenumber[1]</td>
<td>$email[1]</td>
<td>$city[1]</td></tr>
<tr><td>$firstname[2]</td>
<td>$lastname[2]</td>
<td>$mobilenumber[2]</td>
<td>$email[2]</td>
<td>$city[2]</td></tr>
<tr><td>$firstname[3]</td>
<td>$lastname[3]</td>
<td>$mobilenumber[3]</td>
<td>$email[3]</td>
<td>$city[3]</td></tr>
<tr><td>$firstname[4]</td>
<td>$lastname[4]</td>
<td>$mobilenumber[4]</td>
<td>$email[4]</td>
<td>$city[4]</td></tr>
</table>";
?>
Need a code for even odd number in php
ReplyDeleteFollowing code must help..
ReplyDeletefunction is_odd($num) {
ReplyDeleteif ($num % 2 == 0) {
return false;
} else {
return true;
}
}
############################
if (is_odd(4)) {
echo "This number is odd.";
} else {
echo "This number is even.";
}
écrire un programme qui permet de faire la saisie d'un tableau de taille n, sagit l'insertion d'un entier X à une position P !!!!!!!!!
ReplyDelete"java"
Sorry dude I can't understand your language...Can u translate in English Please....!!!
ReplyDeleteif its french...from translation my answer is
ReplyDelete"all array size in Java ARE defined
the .length return that size but you have to init the array first
int x[]; is not enough you need
int x[] = new int[50];
then x.length will be 50 "
package javaapplication1;
ReplyDeletepublic class JavaApplication1 {
public static void main(String[] args) {
int x[]={1,2,3,4,6};
int num=5;
JavaApplication1 j=new JavaApplication1();
j.insert(x,4,5);
}
public void insert(int a[],int pos,int num)
{
int i=0;
int n=a.length;
int x[]=new int[n+1];
for(i=0;i<pos;i++)
{
x[i]=a[i];
}
x[i]=num;
for(int j=i+1;j<x.length;j++)
{
x[j]=a[j-1];
}
for(i=0;i<x.length;i++)
{
System.out.print(x[i]);
}
}
}