specific code help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

specific code help

Post by bawla »

i went to php.net and i was reading the manul which is very very very very long 8O and i read some things that i couldn't understand very well

below are the things i dont understand, if you could please explain it clrearly and as simple as posibile with a example if you could
----------------------------------------------------------------------------
strpos()
strlen()
foo - i've seen it in a few examples but i dont know why its being used
boolean - i know it has to do with something being FALSE or TRUE
array - totally dont get this one
object
session
function - can you set your own function or wut????
. - when sometime in a code they have $a = 3 and $b .=$a
----------------------------------------------------------------------------

thats all that i have for now :cry: :roll:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Here's a quick, simple explanation of some of them. Whilst some arn't technically correct they give the general idea (i hope) ;)

Code: Select all

<?php

//strpos (string position) example
//the below echo's 1 as the e is in position 1 (the h is 0)
echo strpos('hello', 'e');

//strlen (string length) example
//the below echo's 5, as hello has 5 characters in it
echo strlen('hello');

//foo
//See http://catb.org/~esr/jargon/html/F/foo.html

//boolean
//either true or false
//see http://php.net/manual/en/language.types.boolean.php

//array
//an array is just a 'collection' of values
$array = array('john','paul','george','ringo');
echo $array[3]; //will echo ringo
echo $array[0]; //will echo john

//object
//not going to go there ......

//session
//think i'll avoid this too for now 

//function
//here's how to define and call/run your own function
function testing($name){
  //$name is a variable and we can pass whatever value we like into it
  echo 'Hello '.$name;
}
testing('fred'); //this will display Hello fred
testing('woooot'); //this will display Hello woooot

//. (concatenate)
//the . basically means append .. or add onto the end of
$a = 'hello ';
$a .= 'fred'; //this 'adds' fred onto the end of $a (which is hello)
echo $a; //echo's hello fred

?>
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

You can probably benefit a lot from a tutorial. Have you tried this one: php.net/tut. I also recommend the tutorials at phpfreak.com. Sorry I don't have time to give examples but I know you'll get it after going through a simple tutorials. Maybe someone else will have the time to write som sample scripts?

Edited: I guess somebody did :D

I agree with mark that you probably should learn some more before going into sessions and objects.

arrays are cool
Last edited by andre_c on Mon Mar 22, 2004 9:35 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i suppose i can tackle a few of those before I go to bed

Yes you can make your own function.

strlen will just count how many items make up a string, ie:

Code: Select all

<?php
$var = "ilovephp";
$count = strlen($var);
echo $count;
// this will display 8, i l o v e p h p (count em, 8 letters)
?>
An array in generic terms is basically a variable that can/does have multiple values 0 is actually 1 in a array, ie:

Code: Select all

$string  = array('1', '2', '3', '4');
echo $string[0]; 
// this will echo 1.
Foo[bar] is usually just text they choose to show examples, which alot of the functions u asked for show examples of how they work on php.net.

http://php.net/manual/en/language.types.boolean.php, explained good there.

thats all i'm going to hit, bed time for me.
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

whoa, this is very good information, thank you all, i understand most of those now since it was laid out simply (simple things for simple minds, lol) i love this place
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

... also the . means concatenate so:

Code: Select all

$sentence1 = 'The fox jumped ';
$sentence2 = 'over ';
$sentence3 = 'the fence';

$complete = $sentence1 . $sentence2 . $sentence3;
echo $complete; //outputs: The fox jumped over the fence
// or you can just

echo  $sentence1 . $sentence2 . $sentence3;
I guess i did have a little time
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

wut if there are 2 [dots]'s

for example

Code: Select all

.$row['username'].
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Same thing.

Code: Select all

$one = 'one';
$two = 'two';
$three = 'three';
echo $one.' '.$three.' '.$two; //outputs one three two
Above there are 5 things being 'concatenated' together, 3 variables plus 2 spaces.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

add whatever is next
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

Code: Select all

if(mysql_num_rows($result)){
  $row = mysql_fetch_assoc($result);
  echo 'Username: '.$row['username']."<br>\n";
theres nothing to add
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

echo 'Username: '.$row['username']."<br>\n";
There you are 'adding' (adding in the appending sense, not the mathematical sense) 3 things together, some text, a variable and a '<br>'
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

o alright, i get it, thanks
Post Reply