Class And Object :PHP

Tags

, , ,

Class is a user defined data type which is collection of properties,methods and constant while object is an instance of class.

<html>

<body>

<?php

Class pqr

{

var $a = “SynapseIndia”;

}

$obj1 = new pqr();//object 1 for class pqr

$obj2 = new pqr();//object2 for class pqr

$obj2->a = “Noida!”;

echo “$obj1->a<br />”;

echo “$obj2->a<br />”;

?></body>

</html>
SynapseIndia claim top notch services in the field of IT sectors within tight deadlines.Employee at SynapseIndia never gets complaints from clients for deadlines, So, it is one of the trusted offshore software development company, that is easily completed the projects with a minimum span of time. IT Mentor & an Entrepreneur SynapseIndia CEO Shamit Khemka is responsible to make this organization robust and reliable.You can check feedback and reviews given by clients on various projects completed by us.

The Employment Bond is basically an agreement which the company and the employee enter into which among the other terms contained therein states that in consideration of the training given to the Employee and the money spent by the company in imparting such training, the Employee will remain in the services of the company for a particular period.

Static Properties :PHP

Tags

, , ,

Static properties are those whose value cannot be reset.We cannot access it through an object because it belongs to class so we can access it through class name only by using :: scope resolution operator.

<?php
class too
{
public static $my_static = ‘foo’;

public function staticValue() {
return self::$my_static;
}
}

class Bar extends too
{
public function fooStatic() {
return parent::$my_static;
}
}
print too::$my_static . “\n”;

$too = new too();
print $too->staticValue() . “\n”;
print $too->my_static . “\n”;

print $too::$my_static . “\n”;
$classname = ‘too’;
print $classname::$my_static . “\n”;

print Bar::$my_static . “\n”;
$bar = new Bar();
print $bar->fooStatic() . “\n”;
?>

SynapseIndia is a leading website and mobile app development company. IT Mentor & an Entrepreneur SynapseIndia CEO Shamit Khemka spread their business across the globe. They have more than 500 full time employees and all the experts at SynapseIndia working in tandem to achieve good feedback from clients having no complaints at all. They have already delivered more than 3000 thousand projects. They pass on complete quality solutions for website development services and getting good Reviews on various SynapseIndia PHP Web development projects.

The Employment Bond is basically an agreement which the company and the employee enter into which among the other terms contained therein states that in consideration of the training given to the Employee and the money spent by the company in imparting such training, the Employee will remain in the services of the company for a particular period.

HTTP HEADERS IN PHP

Tags

, , ,

Http headers in PHP are used to send extra informations like type of programm making the request,date requested,length of the documents and many more.It is also used for detecting the status information like page was found or not(404 errors),location of the document.There are n numbers of header which can ne used in php,but let us understand one of the most important header i.e location which is used to redirect user from one page to another page.Header from server side is thrown to the client side by using header function.

Let us see the implementation of location header in PHP:

<?PHP

header(“location: http://www.synapseindia.careers/&#8221;);

?>

<html>

<body>
</body>

</html>

To turn the ball in your court you must grasp this program well.

SynapseIndia never abuse to any employee or its client. SynapseIndia always against employee harassment.

You can check reviews given by clients on various projects completed by us.

SynapseIndia Part 2 – Displaying Information & Variables Php Development

Tags

, ,

Introduction

In the last part of the tutorial I explained some of the advantages of PHP as a scripting language and showed you how to test your server for PHP. In this part I will show you the basics of showing information in the browser and how you can use variables to hold information.

Printing Text

To output text in your PHP script is actually very simple. As with most other things in PHP, you can do it in a variety of different ways. The main one you will be using, though, is print. Print will allow you to output text, variables or a combination of the two so that they display on the screen.

The print statement is used in the following way:

print(“Hello world!”);

I will explain the above line:

print is the command and tells the script what to do. This is followed by the information to be printed, which is contained in the brackets. Because you are outputting text, the text is also enclosed instide quotation marks. Finally, as with nearly every line in a PHP script, it must end in a semicolon. You would, of course, have to enclose this in your standard PHP tags, making the following code:

<?
print(“Hello world!”);
?>

Which will display:

Hello world!

on the screen.

Variables

As with other programming languages, PHP allows you to define variables. In PHP there are several variable types, but the most common is called a String. It can hold text and numbers. All strings begin with a $ sign. To assign some text to a string you would use the following code:

$welcome_text = “Hello and welcome to my website.”;

This is quite a simple line to understand, everything inside the quotation marks will be assigned to the string. You must remember a few rules about strings though:

Strings are case sensetive so $Welcome_Text is not the same as $welcome_text
String names can contain letters, numbers and underscores but cannot begin with a number or underscore
When assigning numbers to strings you do not need to include the quotes so:

$user_id = 987

would be allowed.

Outputting Variables

To display a variable on the screen uses exactly the same code as to display text but in a slightly different form. The following code would display your welcome text:

<?
$welcome_text = “Hello and welcome to my website.”;
print($welcome_text);
?>

As you can see, the only major difference is that you do not need the quotation marks if you are printing a variable.

Formatting Your Text

Unfortunately, the output from your PHP programs is quite boring. Everything is just output in the browser’s default font. It is very easy, though, to format your text using HTML. This is because, as PHP is a server side language, the code is executed before the page is sent to the browser. This means that only the resulting information from the script is sent, so in the example above the browser would just be sent the text:

Hello and welcome to my website.

This means, though, that you can include standard HTML markup in your scripts and strings. The only problem with this is that many HTML tags require the ” sign. You may notice that this will clash with the quotation marks used to print your text. This means that you must tell the script which quotes should be used (the ones at the beginning and end of the output) and which ones should be ignored (the ones in the HTML code).

For this example I will change the text to the Arial font in red. The normal code for this would be:

<font face=”Arial” color=”#FF0000″>
</font>

As you can see this code contains 4 quotation marks so would confuse the script. Because of this you must add a backslash before each quotation mark to make the PHP script ignore it. The code would chang
e to:

<font face=\”Arial\” color=\”#FF0000\”>
</font>

You can now include this in your print statement:

print(“<font face=\”Arial\” color\”#FF0000\”>Hello and welcome to my website.</font>”);

which will make the browser display:

Hello and welcome to my website.

because it has only been sent the code:

<font face=”Arial” color=”#FF0000″>Hello and welcome to my website.</font>

This does make it quite difficult to output HTML code into the browser but later in this tutorial I will show you another way of doing this which can make it a bit easier.

SynapseIndia Php Development -cookies – background

Tags

, ,

Cookies have been around for quite some time on the internet. They were invented to allow webmaster’s to store information about the user and their visit on the user’s computer.

At first they were feared by the general public because it was believed they were a serious privacy risk. Nowadays nearly everyone has cookies enabled on their browser, partly because there are worse things to worry about and partly because all of the “trustworthy” websites now use cookies.

This lesson will teach you the basics of storing a cookie and retrieving a cookie, as well as explaining the various options you can set with your cookie.

creating your first php cookie

When you create a cookie, using the function setcookie, you must specify three arguments. These arguments are setcookie(name, value, expiration):

  1. name: The name of your cookie. You will use this name to later retrieve your cookie, so don’t forget it!
  2. value: The value that is stored in your cookie. Common values are username(string) and last visit(date).
  3. expiration: The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted.

In this example we will be creating a cookie that stores the user’s last visit to measure how often people return to visit our webpage. We want to ignore people that take longer than two months to return to the site, so we will set the cookie’s expiration date to two months in the future!

PHP Code:

<?php
//Calculate 60 days in the future
//seconds * minutes * hours * days + current time
$inTwoMonths = 60 * 60 * 24 * 60 + time(); 
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths); 
?>

Don’t worry if you can’t follow the somewhat involved date calculations in this example. The important part is that you know how to set a cookie, by specifying the three important arguments: name, value and expiration date.

retrieving your fresh cookie

If your cookie hasn’t expired yet, let’s retrieve it from the user’s PC using the aptly named $_COOKIE associative array. The name of your stored cookie is the key and will let you retrieve your stored cookie value!

PHP Code:

<?php
if(isset($_COOKIE['lastVisit']))
	$visit = $_COOKIE['lastVisit']; 
else
	echo "You've got some stale cookies!";

echo "Your last visit was - ". $visit;
?>

This handy script first uses the isset function to be sure that our “lastVisit” cookie still exists on the user’s PC, if it does, then the user’s last visit is displayed. If the user visited our site on February 28, 2008 it might look something like this:

Display:

Your last visit was – 11:48 – 02/28/08

SynapseIndia Php Development – sessions – why use them?

Tags

, ,

As a website becomes more sophisticated, so must the code that backs it. When you get to a stage where your website need to pass along user data from one page to another, it might be time to start thinking about using PHP sessions.

A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which requires data(the user’s selected product) to be remembered from one page to the next.

php sessions – overview

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.

It is important to ponder if the sessions’ temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users’ data from getting confused with one another when visiting the same webpage.

Note:If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.

starting a php session

Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent.

Below is a simple script that you should place at the beginning of your PHP code to start up a PHP session.

PHP Code:

<?php
session_start(); // start up your PHP session! 
?>

This tiny piece of code will register the user’s session with the server, allow you to start saving user information and assign a UID (unique identification number) for that user’s session.

storing a session variable

When you want to store user data in a session use the $_SESSIONassociative array. This is where you both store and retrieve session data. In previous versions of PHP there were other ways to perform this store operation, but it has been updated and this is the correct way to do it.

PHP Code:

<?php
session_start(); 
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

Display:

Pageviews = 1

In this example we learned how to store a variable to the session associative array $_SESSION and also how to retrieve data from that same array.

php sessions: using php’s isset function

Now that you are able to store and retrieve data from the $_SESSION array, we can explore some of the real functionality of sessions. When you create a variable and store it in a session, you probably want to use it in the future. However, before you use a session variable it is necessary that you check to see if it exists already!

This is where PHP’s isset function comes in handy. isset is a function that takes any variable you want to use and checks to see if it has been set. That is, it has already been assigned a value.

With our previous example, we can create a very simple pageview counter by using isset to check if the pageview variable has already been created. If it has we can increment our counter. If it doesn’t exist we can create a pageview counter and set it to one. Here is the code to get this job done:

PHP Code:

<?php
session_start();  
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views']; 
?>

The first time you run this script on a freshly opened browser the if statement will fail because no session variable views would have been stored yet. However, if you were to refresh the page the if statement would be true and the counter would increment by one. Each time you reran this script you would see an increase in view by one.

cleaning and destroying your session

Although a session’s data is temporary and does not require that you explicitly clean after yourself, you may wish to delete some data for your various tasks.

Imagine that you were running an online business and a user used your website to buy your goods. The user has just completed a transaction on your website and you now want to remove everything from their shopping cart.

PHP Code:

<?php
session_start();  
if(isset($_SESSION['cart']))
    unset($_SESSION['cart']); 
?>

You can also completely destroy the session entirely by calling thesession_destroy function.

PHP Code:

<?php
session_start(); 
session_destroy();
?>

Destroy will reset your session, so don’t call that function unless you are entirely comfortable losing all your stored session data!

SynapseIndia Php Development-For Loop

Tags

, ,

The for loop is simply a while loop with a bit more code added to it. The common tasks that are covered by a for loop are:

  1. Set a counter variable to some initial value.
  2. Check to see if the conditional statement is true.
  3. Execute the code within the loop.
  4. Increment a counter at the end of each iteration through the loop.

The for loop allows you to define these steps in one easy line of code. It may seem to have a strange form, so pay close attention to the syntax used!

for loop example

Let us take the example from the while loop lesson and see how it could be done in a for loop. The basic structure of the for loop is as follows:

Pseudo PHP Code:

for ( initialize a counter; conditional statement; increment a counter){
	do this code;
}

Notice how all the steps of the loop are taken care of in the for loopstatement. Each step is separated by a semicolon: initiliaze counter, conditional statement, and the counter increment. A semicolon is needed because these are separate expressions. However, notice that a semicolon is not needed after the “increment counter” expression.

Here is the example of the brush prices done with a for loop .

PHP Code:

$brush_price = 5; 

echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
for ( $counter = 10; $counter <= 100; $counter += 10) {
	echo "<tr><td>";
	echo $counter;
	echo "</td><td>";
	echo $brush_price * $counter;
	echo "</td></tr>";
}
echo "</table>";

Display:

Quantity Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500

It is important to note that both the for loop and while loop implementation of the price chart table are both OK at getting the job done. However, the for loop is somewhat more compact and would be preferable in this situation. In later lessons we will see where the while loop should be used instead of the for loop.

SynapseIndia Php Development – while loop

Tags

, ,

Repetitive tasks are always a burden to us. Deleting spam email, sealing 50 envelopes, and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks are conquered in the loop

The idea of a loop is to do something over and over again until the task has been completed. Before we show a real example of when you might need one, let’s go over the structure of the PHP while loop.

simple while loop example

The function of the while loop is to do a task over and over as long as the specified conditional statement is true. This logical check is the same as the one that appears in a PHP if statement to determine if it is true or false. Here is the basic structure of a PHP while loop:

Pseudo PHP Code:

while ( conditional statement is true){
	//do this code;
}

This isn’t valid PHP code, but it displays how the while loop is structured. Here is the break down of how a while loop functions when your script is executing:

  1. The conditional statement is checked. If it is true, then (2) occurs. If it is false, then (4) occurs.
  2. The code within the while loop is executed.
  3. The process starts again at (1). Effectively “looping” back.
  4. If the conditional statement is false, then the code within is not executed and there is no more looping. The code following the while loop is then executed like normal.

a real while loop example

Imagine that you are running an art supply store. You would like to print out the price chart for number of brushes and total cost. You sell brushes at a flat rate, but would like to display how much different quantities would cost. This will save your customers from having to do the mental math themselves.

You know that a while loop would be perfect for this repetitive and boring task. Here is how to go about doing it.

Pseudo PHP Code:

$brush_price = 5; 
$counter = 10;

echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
while ( $counter <= 100 ) {
	echo "<tr><td>";
	echo $counter;
	echo "</td><td>";
	echo $brush_price * $counter;
	echo "</td></tr>";
	$counter = $counter + 10;
}
echo "</table>";

Display:

Quantity Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500

Pretty neat, huh? The loop created a new table row and its respective entries for each quantity, until our counter variable grew past the size of 100. When it grew past 100 our conditional statement failed and the loop stopped being used. Let’s review what is going on.

  1. We first made a $brush_price and $counter variable and set them equal to our desired values.
  2. The table was set up with the beginning table tag and the table headers.
  3. The while loop conditional statement was checked, and $counter (10) was indeed smaller or equal to 100.
  4. The code inside the while loop was executed, creating a new table row for the price of 10 brushes.
  5. We then added 10 to $counter to bring the value to 20.
  6. The loop started over again at step 3, until $counter grew larger than 100.
  7. After the loop had completed, we ended the table.

You may have noticed that we placed slashes infront the quotations in the first echo statement. You have to place slashes before quotations if you do not want the quotation to act as the end of the echo statement. This is called escaping a character and it is discussed in our PHP Strings lesson.

With proper use of loops you can complete large tasks with great ease.

SynapseIndia php Development Tutorial – Learn Php

Tags

Php – what is it?

Taken directly from PHP’s home, PHP.net, “PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.”

This is generally a good definition of PHP. However, it does contain a lot of terms you may not be used to. Another way to think of PHP is a powerful, behind the scenes scripting language that your visitors won’t see!

When someone visits your PHP webpage, your web server processes the PHP code. It then sees which parts it needs to show to visitors(content and pictures) and hides the other stuff(file operations, math calculations, etc.) then translates your PHP into HTML. After the translation into HTML, it sends the webpage to your visitor’s web browser.

php – what’s it do?

It is also helpful to think of PHP in terms of what it can do for you. PHP will allow you to:

  • Reduce the time to create large websites.
  • Create a customized user experience for visitors based on information that you have gathered from them.
  • Open up thousands of possibilities for online tools. Check out PHP – HotScriptsfor examples of the great things that are possible with PHP.
  • Allow creation of shopping carts for e-commerce websites.

what you should know

Before starting this tutorial it is important that you have a basic understanding and experience in the following:

  • HTML – Know the syntax and especially HTML Forms.
  • Basic programming knowledge – This isn’t required, but if you have any traditional programming experience it will make learning PHP a great deal easier.

tutorial overview

This tutorial is aimed at the PHP novice and will teach you PHP from the ground up. If you want a drive-through PHP tutorial this probably is not the right tutorial for you.

Remember, you should not try to plow through this tutorial in one sitting. Read a couple lessons, take a break, then do some more after the information has had some time to sink in.

SynapseIndia Php Development Mysql blog tutorial, creating a simple and secured blog

Tags

php mysql blog

tutorial we will talk about creating a blog on php and mysql

Table of contest:
1. Connecting to mysql database
2. Creating mysql tables
3. Coding the blog

Download

Connecting to mysql database is simple, in just few lines of code

Before we start i suggest you read Php Mysql posting to database for better understanding of mysql.

<?php
// connecting to host
mysql_connect(“host”, “user”, “password”); // replace this with your data
mysql_select_db(“your_database”); // database name here
?>

Creating database, tables and columns
Now let’s build the mysql table.
First build a database if you haven’t, and dont forget to edit the connection details at the top.
Click on databases

php mysql blog
Set a name to it and click create.
Now we must build the tables:
mysql create tables
Click ‘go’, a new table will popup, complete those fields like in the pocture below:
Scroll down and click save.
Last one, click on posts and insert tab at top, write something to title, and id live empty.
Now lets continue coding the blog

<?php
// connecting to host
mysql_connect(“host”, “user”, “password”); // replace this with your data
mysql_select_db(“your_database”); // database name here

// lets build a blog
if(isset($_GET[‘post’])){
$post = $_GET[‘post’];

// first of all we will check string length
if(strlen($post) > 11){ // if post id is bigger than 11 charachters
die(‘Blog post nof found.’);
}
// now we will make sure that the post id is numeric and this is a nice security method
if(is_numeric($post)){ // is numeric allows numbers only
$post = (int)$post; // and the int function, which replace every
// string to its correspoing number

// for the tutorial im gonna add mysql_real_escape_string
// but is not really needed in this case
$post = mysql_real_escape_string($post); // final sqli defense

// final part
$query = mysql_query(“SELECT title FROM posts WHERE id=$post LIMIT 1”);
while($row = mysql_fetch_array($query)){
echo $row[‘title’];
}

}
else{ // if post is not numeric then
die(‘Blog post nof found.’); // post does not exist
}

}
else{
// if post is not submitted display them all
$query = mysql_query(“SELECT title, id FROM posts”);
while($row = mysql_fetch_array($query)){
echo $row[‘title’];
echo $row[‘id’];
echo ‘<a href=”?post=’.$id.'”>’.$title.'</a><br>’;
}
}
?>Few last tips!

I used LIMIT 1 on the first mysql query.
That’s done for 2 main pruposes:
1. we are only displaying 1 result
2. it’s a nice speed optinization for mysql
To display blog posts by newest ones replace

Follow

Get every new post delivered to your Inbox.

Join 1,246 other followers