String Arrays Not Like C/C++

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
Baka2002
Forum Newbie
Posts: 5
Joined: Wed Oct 02, 2002 8:56 pm

String Arrays Not Like C/C++

Post by Baka2002 »

i hope this has not been addressed elsewhere i couldnt find anythink usefull in the man.

im used to c/c++ null terminated arrays

like this
$a = "abc"
this would be
a b c \0

however in php i wanted to do this

Code: Select all

$teststr = "lala";
$teststrї1] = '\0';
echo "THIS IS $teststr<br>";
making the string only 1 char long

the c equiv. works like this but i have had no luck in php

any help appreciated
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post by Phirus »

Not sure...but have a look at http://www.php.net/substr/

Code: Select all

&lt;?php

	$teststr = "lala";

	$teststr = substr($teststr,0,1);

	echo $teststr;

?&gt;

Phirus
Baka2002
Forum Newbie
Posts: 5
Joined: Wed Oct 02, 2002 8:56 pm

not what i wanted

Post by Baka2002 »

thanks but its not what i wanted

i want to beable to dump chars to an array and make it a string

i seen those settype and gettype functions but they dont seam to work for what im doing

Code: Select all

$array = "";

$array&#1111;0]='a';
$array&#1111;1] ='\0';
echo $array;
thanks anyway
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

i think the point is that php doesnt use strings the way c++ does...
a string is a string, there is no index. if for whatever strange reason you wont/cant to use substr, then i guess you will just have to loop the string a char at a time into an array
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you want to make an array into a string try implode().

Mac
User avatar
d3ez
Forum Newbie
Posts: 6
Joined: Tue Oct 15, 2002 11:56 am
Location: VA

Post by d3ez »

or vice versa a string to array: explode ();
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

php doesn't use zero-terminated strings.
It's more like i.e. STL-string or BSTR-type. The length is stored and the data and no matter what the data is the string is length characters long.
You can access single chars with

Code: Select all

$str{0}; $str{1}; ... $str{strlen($str)-1}
but $str{1}=0 doesn't make the string shorter in output
ComputerNerd8888
Forum Newbie
Posts: 1
Joined: Sun Sep 21, 2003 6:36 pm

...

Post by ComputerNerd8888 »

PHP dose not use C/C++ style strings well C dose not use C++ style strings (in a way) either...

Because C uses arrays there is now a library for C++ with a struct named string that handles that for use... and because C/C++ (either by hand or not) uses arrays it needs a null terminater...

But in the case of PHP it dose not... There are no variable types all most anything gose...

So try this:

Code: Select all

$teststr = "lala";
echo "THIS IS $teststr";
That is it see I made a string with a variable that could be a integer or anything... It's that simmple... PHP is like C/C++ in some ways but much different in other!
want to be able to dump chars to an array and make it a string
In this case just dump the chars into the varable... There than are str_* function you can use with that varable... Download the Php Manual from http://www.php.net if you don't have it and search str_ that will give you are the string function you can use with that variable (or any variable in PHP in that case)


Keep on coding!

Andrew
Post Reply