Make a New Variable From String In PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this script in PHP like this:
echo "The results is $res";
And now, I want make a new variable to save that script. What can I do?
I do this because the value of $res is always changing depending on the input entered from user.

If I've understood you correctly, you want to save the script into a variable (let's call it $script), so that when $res="foo", the result of echo $script; is "The result is foo" and when $res="bar", echo $script; returns "The result is bar".
I don't think you can do this using a normal variable, but you certainly could do it as a function:
//The function
function get_res($res){
return "The result is $res";
}
//The function call
$res = "foo";
print get_res($res);
$res = "bar";
print get_res($res);
This would output "The result is foo" the first time the function is called, and "The result is bar" the second time.

$res = 'some resource';
$echo = 'The result is ' . $res;
echo $echo;
Is that what you want? If not, please clarify / simplify your question.

Related

PHP Reading the URL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I make the following url to be read:
localhost/index.php?errormsg=wrongpassword
so I want it to set the variable $errormsg to 'wrongpassword' how do I do that? I am pretty sure there is a way using $_GET or $_POST?
You want to use $_GET['errormsg'].
Something like this should do:
if isset($_GET['errormsg'])
{
echo $_GET['errormsg'];
}
$errormsg = $_GET['errormsg']
This should do the trick for you
$_GET['errormsg'] is the answer of your question.
print_r($_GET)
to see all variables in the URL. in this case you want
$_GET['errormsg']
You were as close as ever. A single google would've yielded the answer to you :)
http://php.net/manual/en/reserved.variables.get.php
<?php
// use htmlspecialchars() to clean up url-encoded characters
$error_message = htmlspecialchars($_GET["errormsg"]);
echo 'Error: ' . $error_message;
?>
prints: "Error: error_here", if url = ...?errormsg=error_here

Access to POST vars in php with string name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi, I have a problem when try access to $_POST vars in php. I have a combo with this name "c012". Well, I send the form with this var, and I have checked this var is send ok, and when I try access with this code, where $var1, $var2 and $var3 are numbers:
$var1 = 0;
$var2 = 1;
$var3 = 2;
$pointer_combo = "c".$var1.$var2.$var3;
echo $_POST['$pointer_combo'];
Don't show anything, but if I try this:
echo $_POST['c012'];
Works, and show the value. Whats the problem with code above?
If you are using a dynamic index (index value stored in a variable), you don't need the quotes.
Try this:
echo $_POST[$pointer_combo];
PHP won't do variable substitution if the value is in single quotes. Only double quotes or no quotes. So
echo $_POST[$pointer_combo];
Would work, as would:
echo $_POST["$pointer_combo"];
(But obviously in that second example there isn't much point in the quotes being there!)
Lose the quotes:
$_POST[$pointer_combo];

PHP Code Session Not Working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My PHP code dosnt seem to be working and I dont Know Why when Ever I Use this code it will Make The whole webpage appear white.I think the problem is Here Somewhereecho Hello, (.$_SESSION['username'], ENT_QUOTES, 'UTF-8');Thanks In Advance
session_start();
if(!isset($_SESSION['user']) && empty($_SESSION['user'])) {
echo '<b>Log In</b>';
}
else {
echo Hello, (.$_SESSION['username'], ENT_QUOTES, 'UTF-8');
echo '</br></b>';
echo '<b>Log Out</b>';
}
When your page goes white, it usually means you have a fatal error in your code and need to check your logs, or turn on error_reporting.
In this case you're missing quotes, have the concatenation a bit messed up, and appear to be missing a function call (probably htmlspecialchars).
Also, you're checking $_SESSION['user'] a few lines before in your code, are you sure you don't mean to echo that here instead of $_SESSION['username']?
I think you want to change that line to:
echo "Hello, " . htmlspecialchars($_SESSION['user'], ENT_QUOTES, 'UTF-8');

How do i extract a number from a url using PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}

IF equals something then show output in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Ok I'm not a PHP editor by default I can do a bit but I am struggling witht his simple variable. I want to display something if the value isnt set to null. The output is run by a function i think, its a catgory for a listing so it would say something like:
WHATS ON: Music, Dance, Trance
Now if there isnt a category set it just says 'WHATS ON:' where as I dont want anything to show if there isnt a category set. Heres the output to display the 'WHATS ON' and category string:
$featured_event .= "<p class=\"complementaryInfo\"><b style=\"color:#74c1df\">WHATS ON:</b>".system_itemRelatedCategories($event->getNumber("id"), "event", true)."</p>";
and heres my variable attempt:
if (system_itemRelatedCategories($event->getNumber("id"), "event", true) == "") {
$featured_event .= "<p class=\"complementaryInfo\"><b style=\"color:#74c1df\">WHATS ON:</b>".system_itemRelatedCategories($event->getNumber("id"), "event", true)."</p>";
}
If you need any more info please ask, I hope this is enough to give you an idea of my problem.
Thanks
Your theory is almost correct. The only issue is that you are telling it to show "What's on" ONLY if there is nothing on.
Change your == to != and it should work fine. Or just drop the == "" entirely since any non-empty string will be truthy.
Personally, I'd further optimise it to:
if( $events = system_itemRelatedCategories($event->getNumber("id"),"event",true))
$featured_event .= "<p class=\"...\"><b...>WHATS ON:</b> ".$events."</p>";
This avoids calling the same function twice.
Use empty() function - if variable is not set or is empty then it returns true
if (!empty(system_itemRelatedCategories($event->getNumber("id"), "event", true)))

Categories