Learning PHP…

August 23, 2008 at 11:06 am (Programming, Uncategorized)

PHP (recursive acronym for “PHP: Hypertext Preprocessor”) is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

Example 1: Hello World (sample1.php)

<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>

PHP can be used on all major operating systems, including Linux, many Unix variants (including HP-UX, Solaris and OpenBSD), Microsoft Windows, Mac OS X, RISC OS, and probably others. PHP has also support for most of the web servers today. This includes Apache, Microsoft Internet Information Server, Personal Web Server, Netscape and iPlanet servers, Oreilly Website Pro server, Caudium, Xitami, OmniHTTPd, and many others. For the majority of the servers PHP has a module, for the others supporting the CGI standard, PHP can work as a CGI processor.v

Variables and Operators

$var = “Hello”; //string “hello”

$var = 3; // integer 3

$var2 = $var1; // Assigns a value of ‘PHP’ to $var2

echo $var1; // Outputs ‘PHP’

STRINGS

A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible.

<?php
echo “Hello World”;

?>

ARRAYS

An array is a special kind of variable that contains multiple values. The simplest way to create an array in PHP is to use the built-in array function:

$myarray = array(‘one’, 2, ‘3’);

$arrayvar = array(‘apple’, ‘banana’, ‘orange’);

echo $arrayvar[0]; // Outputs ‘apple’

echo $arrayvar[1]; // Outputs ‘banana’

echo $arrayvar[2]; // Outputs ‘orange’

CONTROL STRUCTURES

The most basic, and most often-used, control structure is the if-else statement.

Here’s what it looks like:

1. If condition

Ex: if($username==”admin” and password==”admin”) { echo “Hello admin! Welcome!”; }

else { echo “Invalid username or password”; }

2. While condition

Example:

$count = 1;

while ($count <= 10) {

echo “$count “;

++$count;

}

3. For Loop

Ex: for($i = 0; $i <10; $i++)

{ echo $i; }

Example:

<html>

<body><center>

<table width =”50″ border=”1“>

<?php

for($y=1; $y<=10; $y++)

{ echo “<tr><td><center><h1>$y </h1></center></td></tr>”; }

?>

</table>

</center>

</body></html>

DEALING WITH FORMS

One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element in a form will automatically be available to your PHP scripts.

welcome.html

<form action=”welcome.php” method=”get”>

<label>First Name: <input type=”text” name=”firstname” />

</label><br />

<label>Last Name: <input type=”text” name=”lastname” />

</label><br />

<input type=”submit” value=”GO” />

</form>

welcome.php

<?php

$firstname = $_POST[‘firstname’];

$lastname = $_POST[‘lastname’];

echo “Welcome to my Website, $firstname $lastname!”;

?>

SESSION HANDLING FUNCTIONS

Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

Example:

session_start();

$_SESSION[‘username’] = $name;

To destroy a session you created:

Example: session_destroy();

UPLOADING FILE IN PHP

The <input type="file"> form element lets a user upload the entire contents of a file to your server. When a form that includes a file element is submitted, the PHP interpreter provides access to the uploaded file through the $_FILES auto-global array.

$_FILES[‘upload-name’][‘name’]: The name of the file as uploaded from the client to the server.

$_FILES[‘upload-name’][‘type’]: The MIME type of the uploaded file. Whether this variable is assigned depends on the browser capabilities.

$_FILES[‘upload-name’][‘size’]: The byte size of the uploaded file.

$_FILES[‘upload-name’][‘tmp_name’]: Once uploaded, the file will be assigned a temporary name before it is moved to its final location.

$_FILES[‘upload-name’][‘error’]: An upload status code. Despite the name, this variable will be populated even in the case of success. There are five possible values:

CONNECTING PHP TO MYSQL

1. $connect = mysql_connect(address, username, password); // address is the ip address or host name of the computer. username and password of the same username and password of your mysql server. returns true if the connection is established.

2. mysql_select_db($databasename,$connect); // databasename name of the database, $connect is the name of the connection your established see no.1.

3. mysql_query($query[, connection_id]); // $query is the sql command you want to execute;

4. mysql_close([connection id]) // To close the connection in database server.

Example:

<?php
$link = mysql_connect(‘localhost’, ‘root’, ”);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

Permalink Leave a Comment

Simple HttpServer in Twisted Python

May 27, 2008 at 8:25 am (Programming, Uncategorized)

this is a sample code of http server programmed in twisted python
#httpserver.py
#!/usr/bin/python
import sys
from twisted.protocols import basic
from twisted.web import server,resource,http
from twisted.internet.protocol import Protocol

class HttpProtocol(basic.LineReceiver,Protocol):

def __init__(self):
self.lines = []

def connectionMade(self):
self.sendLine(“HTTP/1.0 200 OK”)
self.sendLine(“Content-Type:text/plain”)
self.sendLine(“”)

def lineReceive(self,line):
# your code goes here: process the line you receive
print line

class HttpFactory(http.HTTPFactory):

def __init__(self):
self.protocol = HttpProtocol

def onClientConnected(self):
print “Connected”

reactor.listenTCP(8080,HttpFactory())
reactor.run()

running on the terminal
$ python httpserver.py
as the reactor.run called, your http server is now listening on port 8080

Permalink Leave a Comment

List of All Programming Languages

May 7, 2008 at 7:34 am (Programming, Uncategorized)

Helloworld in all programming language

ActionScript
AdaLanguage:
AlephLanguage:
AppleScript
AssemblyLanguage for the IBM-PC (i386):
AwkLanguage:
B (BeeLanguage):
BasicLanguage
BefungeLanguage:
Bourne Shell:
BrainfuckLanguage (an EsotericProgrammingLanguage)::
C (CeeLanguage) (according to KernighanAndRitchie):
CsharpLanguage:
C++ (CeePlusPlus)
Cobol
ColdFusion
RM COBOL
COW (an EsotericProgrammingLanguage):
D
DOS (DiskOperatingSystem)
EiffelLanguage
ErlangLanguage:
EtcLanguage:
Forte TOOL:
ForthLanguage:
FrinkLanguage
GoogleplexianLanguage:
HaskellLanguage
HqNinePlusLanguage
HtagLanguage
HTML
InformLanguage
IoLanguage
JavaLanguage:
Swing Java
JavaApplet:
JavaScript:
J (JayLanguage)
JoyLanguage:
K (KayLanguage)
LingoScriptingLanguage
Logo
LolCode:
LuaLanguage:
LxLanguage:
MicrosoftExcel
ModulaThree
NemerleLanguage: (http://www.nemerle.org)
OpalLanguage
ObjectiveCaml
PascalLanguage
PathLanguage
PerlLanguage
PerlInline
PhpLanguage
PlbLanguage (or Databus)
PowerScript
PowerShell
PrographLanguage
Prolog
PythonLanguage
QBasic
Rebol
RexxLanguage
RpgLanguage
RubyLanguage
ShellScript
SchemeLanguage
SmalltalkLanguage
SmlLanguage
SnobolLanguage
TeX
TomLanguage
ToolCommandLanguage
UnLambdaLanguage
VisualBasic
VisualFoxPro
MS VJ++
VoiceXml
Whirl
VBScript
JScript
WikiWiki

Permalink 1 Comment

Start the Midlet when New Message arrived

May 7, 2008 at 5:07 am (Mobile, Programming, Uncategorized)

If you want your midlet to start when incoming message arrived, the message should be sent with specific port where your midlet is listening.

Your jad file should look like this
MIDlet-Jar-URL: MyMidlet-0.0.1.jar
MIDlet-Jar-Size: 556255
MIDlet-Name: MyMidlet
MIDlet-Vendor: 3rd Brand Pte Ltd.
MIDlet-Version: 0.0.1
MIDlet-1: com.MyMidlet
MIDlet-Push-1: sms://:5000,MyMidlet,*
MIDlet-PortNumber: 5000
MIDlet-Permissions: javax.microedition.io.PushRegistry,javax.wireless.messaging.sms.receive

But your midlet does not automatically start when new message arrive.. It will ask you if you want to start your midlet.

Permalink Leave a Comment

Writing J2ME Application in Linux

April 30, 2008 at 2:23 am (Mobile, Programming, Uncategorized)

If you’re going to create a mobile game application better use j2me.. But if you’re mobile application runs in a background better used symbian c++.
Quick Start for setting up your workspace..
1. Install eclipse in synaptic manager for your IDE. Why choose eclipse? Simple and easy to use.
2. You need to have the Java Development Kit (JDK) and the Sun Java Wireless Toolkit (WTK) installed on your system. Click here for installation tutorial
3. Install JDK Sun Java Standard Edition (SE) Downloads .
4. After setting up all the installers, you can now create a simple Hello World.

Creating a project:
1 Choose File – > New -> Project
2. New Poject window will appear. Select J2ME Midlet suite then click Next. Type the name of your project. Example HelloWorld Select which Device your going to use. Then click Finish.
3. On the project explorer pane, right click the name of your project.Select New – > Package. Type the name of your package.
4. After creating a package, right click it, select New -> File . Type your filename for example “HelloWorld.java”

Type this source code.
Create the file src/HelloWorld.java with the following code:

// Sample adapted from http://developers.sun.com/mobility/midp/articles/wtoolkit/
package com.hello
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld
extends MIDlet
implements CommandListener
{
private Form mMainForm;
public HelloWorld()
{
mMainForm = new Form(“HelloWorld”);
mMainForm.append(new StringItem(null, “Hello, World!”));
mMainForm.addCommand(new Command(“Exit”, Command.EXIT, 0));
mMainForm.setCommandListener(this);
}
public void startApp()
{ Display.getDisplay(this).setCurrent(mMainForm); }
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s)
{ notifyDestroyed(); }
}

5. You can now run your HelloWorld midlet.
6. Creating the jar file. Right click the name of your project . Select J2ME then Create Package..

Permalink 3 Comments