Friday, March 13, 2015

HOW TO MAKE THE STAR TRIANGLE IN PHP?

It is very easy to make a STAR Triangle in php .This is a type of matrix in which we can print rows and columns .You can make the STAR triangle by using (for loop) or (foreach) loop in php. For or foreach loop make it very easy in to make STAR Triangle.This can improve the logic of Fresher . Check below the programming syntax in front of output:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
1
2
3
4
5
6
7
8
<?php
for($i=0;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo "*&nbsp;";
}
echo "<br>";
}
?>
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
1
2
3
4
5
6
7
8
9
</pre>
<?php
for($i=0;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*&nbsp;";
}
echo "<br>";
}
?>
 *
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
1
2
3
4
5
6
7
8
<?php for($i=0;$i=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "*"." ";
}
echo "<br>";
}?>
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
1
2
3
4
5
6
7
8
9
10
11
12
<?php
 
for($i=5;$i-->=0;$i--){
for($k=5;$k>=$i;$k--){
echo "  ";
}
for($j=1;$j<=$i;$j++){
echo "*  ";
}
echo "<br>";
}
?>
 *
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " &nbsp;";
}
for($j=1;$j<=$i;$j++){
echo "* &nbsp;";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " &nbsp;";
}
for($j=1;$j<=$i;$j++){
echo "* &nbsp;";
}
echo "<br>";
}
?>




1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1
2
3
4
5
6
7
<?php
for($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo $j;
}
echo “<br>”;
} ?>
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
1
2
3
4
5
6
<?phpfor($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo $i;
}
echo "<br>";
} ?>
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1 1
1
2
3
4
5
6
<?phpfor($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo "1";
}
echo "<br>";
} ?>

PHP Program to print Pyramid of Stars.

Here is another fun PHP program for the school and college kids those who want to print stars of pyramid using PHP technology.
Here is the code and you can see the demo below:
Enter the number in the box to see how it works:
Enter number of lines: 




Tuesday, March 10, 2015

what is difference between mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() in php

Many of the php programming newbies get confused about mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() functions, but all of these functions performs a similar process.
Let us create a table “tb” for clear example with three fields “id”, “username” and “password”
Table:  tb
Insert a new row into the table with values 1 for id, tobby for username and tobby78$2 for password
db.php
1
2
3
4
<!--?<span class="hiddenSpellError" pre=""-->php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?&gt;
 mysql_fetch_row()
Fetch a result row as an numeric array
1
2
3
4
5
6
7
8
9
10
<html>
<!--?<span class="hiddenSpellError" pre=""-->php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_row($query);
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>
Result
1  tobby  tobby78$2
 mysql_fetch_object()
Fetch a result row as an object
1
2
3
4
5
6
7
8
9
10
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_object($query);
echo $row->id;
echo $row->username;
echo $row->password;
?>
</html>
Result
1  tobby  tobby78$2
 mysql_fetch_assoc()
Fetch a result row as an associative array
1
2
3
4
5
6
7
8
9
10
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_assoc($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
?>
</html>

Result
1  tobby  tobby78$2
 mysql_fetch_array()
Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_array($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];

<span style="color: #993300;">/* here both associative array and numeric array will work. */</span>

echo $row[0];
echo $row[1];
echo $row[2];

?>
</html>

Thursday, March 5, 2015

Page redirect after certain time PHP

header( "refresh:5;url=wherever.php" );
this is the php way to set header which will redirect you to wherever.php in 5 seconds

Monday, February 2, 2015

PHP : var_dump() function with Example and description.

Description

The var_dump() function is used to display structured information (type and value) about one or more variables.
var_dump -- Dumps information about a variable

void var_dump ( mixed expression [, mixed expression [, ...]] )

This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.  

Version

(PHP 4 and above)
Syntax

var_dump(variable1, variabl2, ....variablen)

Parameter
Name Description Required /
Optional Type
variable1
variable2
---------
variablen

The variable being checked Required Mixed*

*Mixed : mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Return value

Nothing

Example -1 :
view plainprint?

    <?php
    $var_name1=678;
    $var_name2="a678";
    $var_name3="678";
    $var_name4="W3resource.com";
    $var_name5=698.99;
    $var_name6=+125689.66;          
    echo var_dump($var_name1)."<br>";
    echo var_dump($var_name2)."<br>";
    echo var_dump($var_name3)."<br>";
    echo var_dump($var_name4)."<br>";
    echo var_dump($var_name5)."<br>";
    echo var_dump($var_name6)."<br>";
    ?>        

Output :

int(678)
string(4) "a678"
string(3) "678"
string(14) "W3resource.com"
float(698.99)
float(125689.66)