samoi Posted November 6, 2008 Share Posted November 6, 2008 hello guys how are you? can I merge many vars in only one var? an example: <?php $conn = mysql_connect("localhost", "root", "root"); $db = mysql_select_db("data_base"); $username = $_POST['username']; $password = $_POST['password']; $whatever = "whatever var"; $also_a_var = "whatever added vars"; ?> Can any one explain how can I put all of them in only one var? and explain if there are exceptions for any functions like (isset) or any other functions. Thank you all! Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/ Share on other sites More sharing options...
wildboy211 Posted November 6, 2008 Share Posted November 6, 2008 like this? $whatever = "whatever var"; $also_a_var = "whatever added vars"; whatever2 = array($whatever); $also_a_var2 = array(also_a_var); $my_merged = array_merge(whatever2,also_a_var2); Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-683995 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 Do you mean $var1 ="one"; $var2 = "two"; $var3= $var1." ".$var2; echo $var3;//prints one two or do you mean $var1 ="hello"; $var1 .= " world"; echo $var1; //prints hello world Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-683997 Share on other sites More sharing options...
samoi Posted November 6, 2008 Author Share Posted November 6, 2008 I'm sorry, I think I didn't delievered the question very well. ok I have this code: [which is for registration and so.] <?php $conn = mysql_connect("localhost", "root", "pass"); $db = mysql_select_db("d_base"); $username = $_POST['username']; $pass = $_POST['password']; if(some expression){ // do this } else{ // do this } echo $something_from_the_if_statement ; ?> I'm sorry but I'm a beginner to the PHP. and I'm wondering if I can let this whole code execute by scripting only one var? I'm sorry I really don't know how to explain and that's due my English is not my mother lang, and still a beginner to the PHP. Thank you guys Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684016 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 Ok, i know its not your fault, but the question makes no sense, if i was you i would get it working and post any problems you have doing that. Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684019 Share on other sites More sharing options...
Barand Posted November 6, 2008 Share Posted November 6, 2008 In your example you could just do $all_my_vars = $_POST; Data posted from a form is always set except unset radio button and checkbox values. It may be empty though. Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684021 Share on other sites More sharing options...
samoi Posted November 6, 2008 Author Share Posted November 6, 2008 to be more clear as of the example in my second reply, can I do like this? $merge = array_this(conn, db, username, pass); is this right? or no? Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684022 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 Erm, so you want to merge them all together, then no that wouldn't be usable then. So as i said, just keep it simple for now until you have a better understanding. Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684023 Share on other sites More sharing options...
samoi Posted November 6, 2008 Author Share Posted November 6, 2008 to be more clear as of the example in my second reply, can I do like this? $merge = array_this(conn, db, username, pass); is this right? or no? Guys, I'm just asking and wondering if that's possible! I know I can just include this code or require it. but I'm wondering if I can collect all of these vars and put them all in only one var and let it execute itself Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684024 Share on other sites More sharing options...
samoi Posted November 6, 2008 Author Share Posted November 6, 2008 Erm, so you want to merge them all together, then no that wouldn't be usable then. So as i said, just keep it simple for now until you have a better understanding. thank you man, but as I said I need an explanation why I can't do it? is it because I can't array a function? what do you think? Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684025 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 Then no, you can't if you use it for the connection. You can merge them together then split them into separate vars. But if you join them it will be like $var1 = "hostusernamepassword"; mysql_connect($var1);// which will look like mysql_connect("hostusernamepassword"); which is missing two params Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684026 Share on other sites More sharing options...
champoi Posted November 6, 2008 Share Posted November 6, 2008 you can do it like this: $the_array = array(); $the_array[] = "localhost"; $the_array[] = "bla bla"; $the_array[] = $something; like that, but doing it in this order is usually for a set of information that has no need for signatures or labels. you cant use this like this: $the_array[] = "localhost"; $the_array[] = "root"; $the_array[] = "root"; $conn = mysql_connect($the_array) you should be doing this instead: $the_array['a'] = "localhost"; $the_array['b'] = "root"; $the_array['c'] = "root"; $conn = mysql_connect($the_array['a'],$the_array['b'],$the_array['c']) Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684035 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 I don't think this works $the_array[] = "localhost"; $the_array[] = "root"; $the_array[] = "root"; $conn = mysql_connect($the_array) Because it will do $conn = mysql_connect(array(3)) // or something like this. Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684039 Share on other sites More sharing options...
champoi Posted November 6, 2008 Share Posted November 6, 2008 I don't think this works $the_array[] = "localhost"; $the_array[] = "root"; $the_array[] = "root"; $conn = mysql_connect($the_array) Because it will do $conn = mysql_connect(array(3)) // or something like this. yup, that's what i said... you cant use it like that... i haven't tried it, but i just know it wouldn't work Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684053 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 O' yer , sorry my bad, missed that line. Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-684062 Share on other sites More sharing options...
wildboy211 Posted November 9, 2008 Share Posted November 9, 2008 you could do this $myarr = array("localhost","root","root"); $conn = mysql_connect($myarr[0],$myarr[1],myarr[2]); Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-685829 Share on other sites More sharing options...
blueman378 Posted November 9, 2008 Share Posted November 9, 2008 although why the hassle? if its just for the database details why bother? Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-685916 Share on other sites More sharing options...
DeanWhitehouse Posted November 9, 2008 Share Posted November 9, 2008 Exactly , you are just making it more hassle and more confusing for your self. Stay simple until you have a greater knowledge of PHP Quote Link to comment https://forums.phpfreaks.com/topic/131688-can-i-add-many-var-in-on-var-like-array/#findComment-685929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.