douwei2966 2011-10-12 12:03
浏览 90
已采纳

如何在php中创建一个具有签名void myFunction()的函数

By default php returns NULL for function that doesn't do anything.

For example:

1 <?php
2 
3 function foo() {
4     // Nothing here
5 }
6
7 var_dump(foo()); // the result will be NULL

I'm trying to implement an function that will have direct output and i don't need to return anything from inside the function, i what to obtain something like:

void var_dump()// it just only dumps information.

My function will generate a image but will not return any information. The image will be generated, after processing, using ImageJpeg($image) inside the function. The signature in php manual for imageJpeg is:

imagejpeg — Output image to browser or file

in this way I'll have output image and NULL returned from function. How can i avoid that. How can i obtain ONLY output image?

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • dta25920 2011-10-12 12:26
    关注

    The PHP equivalent of a void function foo() is a regular function that simply has no return statement. Whether this function outputs anything or not is completely independent of that. A function may output something in any variety of ways and return a value, or either or neither. In concrete terms, this is a function with no return value which outputs an image:

    function foo() {
        ...
        imagejpg(...);
    }
    
    foo();
    

    What you may be confusing here is the difference between a return value and output. The output is what the program finally outputs to the user/stout/php://output. A function return value is what the function returns to the caller.

    function foo() {
        echo 'Hello World';
        imagejpg(...);
        return 'foo';
    }
    
    $bar = foo();
    

    This function visibly prints "Hello World" on your screen followed by some binary image. The function returns the string 'foo' and the variable $bar now holds 'foo', but this is not output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程