Page 1 of 1

String Arrays Not Like C/C++

Posted: Thu Oct 17, 2002 9:45 am
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

Posted: Thu Oct 17, 2002 9:55 am
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

not what i wanted

Posted: Thu Oct 17, 2002 10:01 am
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

Posted: Thu Oct 17, 2002 10:05 am
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

Posted: Thu Oct 17, 2002 10:11 am
by twigletmac
If you want to make an array into a string try implode().

Mac

Posted: Thu Oct 17, 2002 10:38 am
by d3ez
or vice versa a string to array: explode ();

Posted: Thu Oct 17, 2002 2:48 pm
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

...

Posted: Sun Sep 21, 2003 6:36 pm
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