main.php
<?php
mb_internal_encoding("UTF-8");
require_once('./TController.php');
TController::doMain($argc, $argv);
return 0;
?>
TController.php
<?php
require_once(dirname(__FILE__)."/A.php");
require_once(dirname(__FILE__)."/../../B.php");
require_once(dirname(__FILE__)."/../../C.php");
require_once(dirname(__FILE__)."/D.php");
require_once(dirname(__FILE__)."/E.php");
class TController
{
public function doMain($argc, $argv)
{
StockPriceDefiner::defineStock();
if($argc != 10) {
$file = "main.php";
echo <<<EOT
usage:
php $file year month day width 500 ana_ana isDiv
ex):
input parameters
EOT;
return;
}
print_r($argv);
}
}
?>
I have tried to php main.php [enter] from the command prompt but the usage above isn't displayed. is there anything incorrect you can spot, please help me.
[UPDATE]
there is no error, just nothing is displayed.
Please enable error report by adding
error_report(E_ALL);
at the start of your main.php file.
Secondly, if TController.php file is all you have pasted, then it certainly doesn't contain any definition of
TravelJpController::doMain()
This should do the trick:
TController::doMain($argc, $argv);
Related
I am expecting the below code to return hi when the project is run.But it's not returning anything .There is no error also.I've started the wamp server. How can i fix this?I'm very new to this. Anyone can tell me this?
functions.php
<?php
function getDate($orderDate, $oderTime) {
return "hi";
}
?>
index.php
<?php
require "vendor/autoload.php";
require "config.php";
require "functions.php";
getDate(var_export($days),var_export($Time));
config.php
<?php
$Time = "1";
$days = ["Sat", "Sun"];
when you use return the return value wil not shown on your screen till you echo it out, like this
echo getDate(var_export($days),var_export($Time));
and by the way you can replace return keyword in your function with echo
in this case you don't need to write echo getDate(var_export($days),var_export($Time));
just type getDate(var_export($days),var_export($Time));
So I have access to a public web API and I'm trying to pull information from it and use it in a PHP if statement. I have tried a few different ways but each time I fail? I will post all of my failed attempts to see if anyone knows what I'm doing wrong...
This is the JSON file;
{
"players": [
{
"SteamId": "76561198074117457",
"CommunityBanned": false,
"VACBanned": true,
"NumberOfVACBans": 1,
"DaysSinceLastBan": 738,
"NumberOfGameBans": 0,
"EconomyBan": "none"
}
]
}
Attempt 1 using a PHP function
(php file)
<?php
function VACBanned($vacban) {
if ($vacban == "false") {
return "";
}
elseif ($vacban == "true") {
return "<p>This user has a vac ban...</p>";
}
}
?>
(index file)
<html>
<body>
<?=VACBanned($json['players'][0]['VACBanned']);?>
</body>
</html>
This would output the following error;
PHP Notice: Undefined index: players in index.php on line 13
I initially thought that the API must not have been correctly connecting, so I went into the PHP file and echoed my user ID like so $json["players"][0]["SteamId"]; and it worked... But when I went and attempted to perform the same thing in the Index <?php echo $json["players"][0]["SteamId"];?> I got the error again? When I did this in the php file $test = $json["players"][0]["SteamId"]; and this in the index <?php echo $test;?>it echoed my steam id?????? I tried to just call the if statement in the index like so
<?php
if ($vac_ban == "false") {
return "123";
}
else {
return "<div class='ban_vac'><p>1 VAC BAN<br>76 Days Ago</p></div>";
}
?>
and $vac_ban would be stated in the php file as = $json["players"][0]["VACBanned"]; but that just made it outpute everything above the <? tag and nothing below it. Note the whole time during this I had the two files connected using include('filename'); and error_reporting(E_ALL); ini_set("display_errors", 1); and the json is decoded json_decode(file_get_contents($api_url), true);
You must decode json before use with PHP:
$array = json_decode($data, true);
Now you can do what you want:
VACBanned($array['players'][0]['VACBanned']);
Take a look: json_decode
So, I worked it out for anyone who might be having this issue, I just put my PHP code into the one document, it looks messy but works :)
$code = 'php statement';
// getting perse error
function perse_error_check($code){
if(eval($code) === "true"){
return "no perse error";
}
if(eval($code) === "false"){
return "perse error found";
}
}
// getting fatal error
function fatal_error_check($code){
.......................................
.......................................
}
Can you help me to complete the second function? Actually I am not sure weather it is possible or not.
The following writes the PHP code to another file.
It then uses command line execution to parse the file and check for erros:
/* Put the code to be tested in a separate file: */
$code = '<?php echo "Test"; ?>';
file_put_contents('check.php', $code);
/* Parse that file and store the message from the PHP parser */
$x = exec( 'php -l check.php');
/* Check the message returned from the PHP parser */
if(preg_match('/Errors parsing/i',$x))
{
echo 'The code has errors!';
}
else
{
echo 'The code looks good!';
}
/* delete the file */
unlink('check.php');
The benefit is that the code doesn't run, it just gets parsed. However I assume you would then write that code to a file and use it... so like others mentioned, be VERY careful. Use things like open_basedir (and test it) to restrict access to specific directories, manually check the code before including it in production... etc.
There is a simple way. Put your PHP code in an another file. For an example: index.php and check.php . Put your PHP code in check.php.
Now in index.php write:
$check_data = file_get_contents("yourhosturl/allotherdirectory/check.php");
if(preg_match("/Fatal error/",$check_data)){
echo "fatal error found";
}
Today I noticed that the missing function dosent show an error when is inside an if. And what to see that error, because is hard for debugging
<?php if(3 >1): ?>
<?php echo missingFunction(3); ?>
<?php else: ?>
<?php echo missingFunction(3); ?>
<?php endif; ?>
If I call the function on its own
// This will show that the function is missing
<?php echo missingFunction(4); ?>
In the beging of the file I have
ini_set('display_errors', 1);
error_reporting(E_ALL);
You can't see error if xy function is inside of a (negative) if - only when its triggered. There is no way.
But you can do (check) file like this, and you should write "questionable/suspicious/hidden/if" functions in manually (hardcoded).. better then nothing though.
$maybe_fns = (function1,function2); //... you should type in `if` functions here, manually
$check = #explode(',', $maybe_fns);
while (list($key,$xxx) = each($check))
{
if(!function_exists($xxx)
{
echo '<b>Warning:</b> No function <b>'.$xxx.'</b><br>';
}
}
May come handy in alpha & beta testings, and its better then reading bunch of functions files for sure.
The existence of a function in PHP is only determined at the very last minute when the interpreter executes the statement.
So if you have a missing function inside a condition that never evaluates to true, no errors will be triggered.
For example:
if (false) {
this_function_does_not_exist(); // this never gets run
}
// no errors
I don't get why I get this error
Fatal error: Class 'ImageJpg' not found
Here is the code that I use
spl_autoload_register(function($class) {
if(file_exists($class))
{
include $class.'.php';
}
});
$n = new ImageJpg();
File ImageJpg.php is in the same dir with the code above.
Here is the content of ImageJpg.php
<?php
class ImageJpg
{
public function __construct()
{
echo 'Image from jpg called';
}
}
if(file_exists($class))
{
include $class.'.php';
}
Should be
if(file_exists($class.'.php'))
{
include $class.'.php';
}
Is there a class named ImageJpg in your ImageJpg.php file? And does file exist?
Try this:
spl_autoload_register(function($class) {
if(file_exists($class.'.php'))
{
include $class.'.php';
if (!class_exists($class))
{
die('required class not present');
}
}
else
{
die('file not found');
}
});
I had issues with file_exists also and I was debugging for like 2 hours and nothing happen even after I've searched all around and also on web. At the end it was a typo: instead userController.php it was userControlller.php the actual file.
Check again (in this order):
Make SURE you don't have typo in your file name (either PHP or file system)
Check your path - ROOT and/or use getcwd()
Temporary print / log your checks to make sure everything matches
Avoid human error :)